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

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

问题描述

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



我在插入一个我的共享实体到我的公共数据库。失败的错误:


会员身份
'Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup'
不存在在元数据
集合中。


它所指的外键存在于普通数据库。但我也是与关系另一边的实体(名为ResidentialAddress)进行合作;我甚至没有包含初始化的其他实体(名为MembersDb)的上下文。但是,这两个模型都编译成相同的程序集。



从查找到ResidentialAddress,没有导航属性。虽然在另一个方向有一个导航属性(我不会坚持 - 只在内存中使用)。



我的 MetadataWorkspace EntityConnection 的code>仅使用该数据库所需数据的SSDL / CSDL / MSL来初始化。我已经确认没有引用该组模式数据中提到的外键。

  var metaAssembly = typeof(CommonDb) 。部件; 
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 [] {m​​etaAssembly});
EntityConnection connection = new EntityConnection(metadata,myDatabaseConnection);

可能的CLUE:当我进入生成的类和删除所有 EdmRelationshipAttribute 属性以及相关模型(MembersDb)中配对的 EdmRelationshipNavigationPropertyAttribute



主要问题:


  1. 那么为什么实体框架正在尝试与一个不属于范围的实体的关系做一些事情,也不会受到插入记录的影响!


  2. 我很高兴有生成的代码删除上面提到的属性,但我仍然希望导航属性保留。我如何改变CSDL来实现?


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

解决方案

模型被写入单独的数据库,那么或许edmx文件不应该彼此了解(关于实体或与不属于它们的实体的关系)。


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


  1. 从edmx +取消自动生成中删除uses,并自己创建类。

  2. 删除usings 从edmx +修改t4模板,在创建类时读取多个edmx。

  3. 将edmx文件复制到一边,这样你就有两组edmxs。 b $ b 3.a.使用设置#1自动生成实体。

    3.b.通过删除使用来修改集#2,并用于生成存储库类(对象集)。

让我知道这些工作之一。



祝你好运,
丹尼。


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.

解决方案

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.

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

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