实体框架 - 插入多个模型和数据库实体 [英] Entity Framework - Inserting entity with multiple models and databases

查看:156
本文介绍了实体框架 - 插入多个模型和数据库实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的域名分割成多个实体框架模型。我有跨越多个模型(称为查找)某些共享实体,但是,这些将被替换的使用使用中的Working随着大型模型,在实体框架。然而,是什么让我的情况稍微更独特的是,我也分开这些车型到多个数据库(每个模型中的一个)。

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:

与身份的成员   Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup   在元数据中不存在   集合。

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

这是外键,它的指不的不可以存在于共同DB。但我也的不可以与实体的关系的另一面工作(名为ResidentialAddress);我也不甚至有可能包含有初始化其他实体(名为MembersDb)的范围内。然而,这两种模式被编译成相同的组件。

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.

有没有导航属性从的查找到ResidentialAddress去的。虽然有在其它方向(我不会持久 - 只使用存储器)导航属性

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

我的 MetadataWorkspace EntityConnection 的CommonDb上下文中明确,只有在SSDL / CSDL / MSL的初始化要求该数据库的数据。我已确认有在该集模式的数据中提到的外键的引用。

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

可能的线索:它的工作,当我去到生成的类,并删除所有的 EdmRelationshipAttribute 属性以及它们的配对 EdmRelationshipNavigationPropertyAttribute 从相关模型(MembersDb)。

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

的主要问题:

  1. 那么,为什么它的实体框架是试图做一些与该是既不在范围和实体之间的关系,也不会受记录的插入!?

  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!?

我很高兴有产生code除去上面提到的属性,但我还是想导航性能保持。我怎么会去改变CSDL实现这一目标?

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?

注:的孩子的车型坚持是不是一个优先事项,也不是他们现在跨数据库外键的完整性。这些数据库采用SQL CE仍然存在,但它们最初是从一个单一的主SQL Server数据库生成的。

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.

推荐答案

如果你的模型的每个部分写入到一个单独的数据库,那么也许EDMX文件应该不知道对方的存在(约实体或关系到做实体不属于他们)。

如何尝试以下方法之一:
(要结束了相同的实体类的每一个部分,而且使EF无视它们之间的连接。)

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. 从EDMX删除usings+取消自动生成和创建类自己。
  2. 从EDMX删除usings+修改T4模板创建的类时读取多个EDMX。
  3. 复制EDMX文件,一边让你有两套edmxs的。
     3.A.使用#1自动生成实体。
     3.B.的修改集#2通过删除usings和用于代库类(objectsets)的。
  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.

祝你好运, 丹尼。

这篇关于实体框架 - 插入多个模型和数据库实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆