[英]Entity Framework - Inserting entity with multiple models and databases
I have my domain split into multiple Entity Framework models. 我将我的域拆分为多个Entity Framework模型。 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 .
我有一些跨多个模型的共享实体(名为Lookup),但是,使用在实体框架中使用大型模型中描述的方法将这些实体替换为“使用”引用。 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.
元数据集合中不存在具有标识“Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup”的成员。
That foreign key that it's referring to does not exist on the "common DB". 它所指的外键在“公共DB”上不存在。 But I'm also not working with the entity on the other side of the relationship (named ResidentialAddress);
但我也没有在关系的另一端(命名为ResidentialAddress)与实体合作; nor do I even have the context that would contain the other entity initialized (named MembersDb).
我甚至没有包含初始化的其他实体的上下文(名为MembersDb)。 However, both models are compiled into the same assembly.
但是,两个模型都编译到同一个程序集中。
There are no navigation properties going from Lookup to ResidentialAddress. 从 Lookup到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. 用于CommonDb上下文的
EntityConnection
的MetadataWorkspace
仅使用SSDL / CSDL / MSL显式初始化该数据库所需的数据。 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). 可能的CLUE:当我进入生成的类并从相关模型(MembersDb)中删除所有
EdmRelationshipAttribute
属性及其配对的EdmRelationshipNavigationPropertyAttribute
时,它确实有效。
Key questions: 关键问题:
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!? 那么为什么实体框架试图对一个既不在范围内也不会受到记录插入影响的实体的关系做一些事情呢?
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?
我将如何改变CSDL来实现这一目标?
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.
这些数据库使用SQL CE保留,但它们最初是从单个主SQL Server数据库生成的。
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). 如果模型的每个部分都写入单独的数据库,那么edmx文件可能不应该彼此了解(关于实体或与不属于它们的实体的关系)。
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.) (最终为每个部分使用相同的实体类,但使EF忘记它们之间的连接。)
Let me know if one of these works. 如果其中一个有效,请告诉我。
Good luck, Danny. 祝你好运,丹尼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.