实体框架 - 插入具有多个模型和数据库的实体

发布于 2024-10-31 01:51:06 字数 1747 浏览 0 评论 0原文

我将我的域分为多个实体框架模型。我有一些跨越多个模型的共享实体(名为 Lookup),但是,这些实体被使用 在实体框架中使用大型模型。然而,让我的案例更加独特的是,我还将这些模型分为多个数据库(每个模型一个)。

我在将共享实体之一插入我的公共数据库时遇到问题。它因错误而失败:

有身份的会员 'Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup' 元数据中不存在 收藏。

它所引用的外键存在于“公共数据库”上。但我也与关系另一方的实体(名为 ResidentialAddress)合作;我什至没有包含初始化的其他实体(名为 MembersDb)的上下文。然而,这两个模型都被编译到同一个程序集中。

没有从 Lookup 到 ResidentialAddress 的导航属性。尽管在另一个方向上有一个导航属性(我不会坚持 - 仅在内存中使用)。

CommonDb 上下文的 EntityConnectionMetadataWorkspace 已显式初始化,仅使用该数据库所需数据的 SSDL/CSDL/MSL。我已经确认没有引用该组架构数据中提到的外键。

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);

可能的线索:当我进入生成的类并从相关模型中删除所有 EdmRelationshipAttribute 属性及其配对的 EdmRelationshipNavigationPropertyAttribute 时,它确实有效(成员数据库)。

关键问题:

  1. 那么为什么实体框架试图对既不在范围内也不会受到记录插入影响的实体的关系执行某些操作!?

  2. 我很高兴生成的代码删除了上述属性,但我仍然希望保留导航属性。我将如何更改 CSDL 来实现这一目标?

注意:“子”模型的持久性不是优先事项,它们现在的跨数据库外键的完整性也不是优先事项。这些数据库使用 SQL CE 进行持久化,但它们最初是从单个主 SQL Server 数据库生成的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-11-07 01:51:06

如果模型的每个部分都写入单独的数据库,那么 edmx 文件可能不应该相互了解(关于实体或与不属于它们的实体的关系)。

尝试以下方法之一怎么样:
(最终为每个部分提供相同的实体类,但使 EF 忽略它们之间的连接。)

  1. 从 edmx 中删除“usings” + 取消自动生成并自己创建类。
  2. 从 edmx 中删除“usings” edmx + 修改 t4 模板以在创建类时读取多个 edmx。
  3. 将 edmx 文件复制到一边,这样您就有两组 edmx。
    3.a.使用 set #1 自动生成实体。
    3.b.通过删除“usings”并用于生成存储库类(对象集)来修改集 #2。

让我知道其中之一是否有效。

祝你好运,
丹尼.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文