Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have my domain split into multiple Entity Framework models. I have some shared entities that span multiple models (named Lookup), however, these are replaced with "using" references using the methods described in Working With Large Models In Entity Framework. However, what makes my case slightly more unique is that I'm also separating these models into multiple databases (one per model).

I'm having a problem inserting one of my shared entities into my common DB. It's failing with the error:

The member with identity 'Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup' does not exist in the metadata collection.

That foreign key that it's referring to does not exist on the "common DB". But I'm also not working with the entity on the other side of the relationship (named ResidentialAddress); nor do I even have the context that would contain the other entity initialized (named MembersDb). However, both models are compiled into the same assembly.

There are no navigation properties going from Lookup to ResidentialAddress. Though there is a navigation property in the other direction (which I won't be persisting - only using in memory).

My MetadataWorkspace for the EntityConnection of the CommonDb context was explicitly initialized with only the SSDL/CSDL/MSL for the data required for that database. I have confirmed there is no references to the foreign key mentioned in that set of schema data.

var metaAssembly = typeof(CommonDb).Assembly;
var schemaResources = new string[]
{ 
    String.Format("res://{0}/Common.ssdl", metaAssembly.FullName), 
    String.Format("res://{0}/Common.csdl", metaAssembly.FullName), 
    String.Format("res://{0}/Common.mdl", metaAssembly.FullName), 
}
MetadataWorkspace metadata = new MetadataWorkspace(schemaResources, new []{ metaAssembly });
EntityConnection connection = new EntityConnection(metadata, myDatabaseConnection);

POSSIBLE CLUE: It does work when I go into the generated classes and remove all of the EdmRelationshipAttribute attributes along with their paired EdmRelationshipNavigationPropertyAttribute from the related models (MembersDb).

Key questions:

  1. So why is it that Entity Framework is trying to do something with the relationship that is for an entity that is neither in scope and nor will it be affected by the insertion of the record!?

  2. I am happy to have the generated code remove the attributes mentioned above, but I still want the navigation properties to remain. How would I go about altering the CSDL to achieve that?

NOTE: Persistence of the "child" models is not a priority, nor is the integrity of their now cross-DB foreign keys. These databases are persisted using SQL CE but they were originally generated from a single master SQL Server database.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
225 views
Welcome To Ask or Share your Answers For Others

1 Answer

If each part of your model is written to a separate database, then perhaps the edmx files should not know about each other (about entities or relationship to entities that do not belong to them).

How about trying one of the following approaches:
(To end up with same entities classes for each part, but make EF oblivious of connections between them.)

  1. Remove the "usings" from edmx + cancel auto generation and create classes yourself.
  2. Remove the "usings" from edmx + modify t4 template to read more than one edmx when creating the classes.
  3. Copy edmx files aside so you have two sets of edmxs.
    3.a. Use set #1 for auto generation of entities.
    3.b. Modify set #2 by removing the "usings" and use for generation of repository classes (objectsets).

Let me know if one of these works.

Good luck, Danny.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...