简体   繁体   English

同时使用 ObjectContext 和 DbContext

[英]Using Both ObjectContext and DbContext

Scenario: Trying to extract and rearange information from one database to an other.场景:试图从一个数据库中提取和重新排列信息到另一个数据库。 DB A has some data I want to get. DB A 有一些我想获取的数据。 I want to store it on DB B in a slightly different structure.我想以稍微不同的结构将它存储在 DB B 上。

DB AI get using an EDMX database generated model so it uses a derivative of ObjectContext. DB AI 使用 EDMX 数据库生成 model,因此它使用 ObjectContext 的派生。 DB BI would like to be Code generated. DB BI 想被代码生成。 So I use the code/model first approach by installing EntityFramework 4.1 via Package manager.因此,我通过 Package 管理器安装 EntityFramework 4.1 来使用代码/模型优先的方法。 So DB B uses a DbContext derivative所以 DB B 使用 DbContext 派生

When I try to store information from DB A to DB B it's says:当我尝试将信息从 DB A 存储到 DB B 时,它说:

Test method RoutIT.Irma.Import.Service.Test.ImportIrma2ProductTests.ImportProducts threw exception: System.ArgumentException: Could not find the conceptual model type for 'Some entity in DB A's EDMX model'测试方法 RoutIT.Irma.Import.Service.Test.ImportIrma2ProductTests.ImportProducts 抛出异常:System.ArgumentException:找不到“DB A 的 EDMX 模型中的某些实体”的概念 model 类型

It actually does it when adding a DB B entity to the DB B's Derived DbContext's DbSet property.在将 DB B 实体添加到 DB B 的 Derived DbContext 的 DbSet 属性时,它实际上是这样做的。 So the code is like所以代码就像

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
            foreach (FirstPVC pvc in pvcs)
            {
                this._irmaImport.FirstPVCs.Add(pvc); <--
                this._irmaImport.SaveChanges();
            }
            scope.Complete();
        }
 }

It happens at the point in the code marked by the arrow above ("<--")它发生在上面箭头标记的代码点(“<--”)

FirstPVC is a DB B property yet at the point of the arrow it complanes about not having a conceptual model for an entity belonging to DB B's context. FirstPVC 是一个 DB B 属性,但在箭头处,它抱怨对于属于 DB B 上下文的实体没有概念上的 model。

This is strange since I try to store an DB B entity to the DB B context.这很奇怪,因为我尝试将 DB B 实体存储到 DB B 上下文中。 Why should it care about entity's of DB A.为什么要关心 DB A 的实体。

All contexts are contained in the same project.所有上下文都包含在同一个项目中。 But DB B's Derived DbContext only has knowledge about it's own DbSet<> properties, suddenly when trying to add something to the DbSet<> property it give me the error in bold above.但是 DB B 的 Derived DbContext 只知道它自己的 DbSet<> 属性,突然在尝试向 DbSet<> 属性添加一些东西时,它给了我上面粗体字的错误。

Anybody know why this happens?有人知道为什么会这样吗? Why should DbContext care about entity's of another context specifically one of a ObjectContext derived class.为什么 DbContext 应该关心另一个上下文的实体,特别是 ObjectContext 派生的 class 之一。

Perhapse it's usefull to note the entity it's complaining about looks a bit like this也许注意到它抱怨的实体看起来有点像这样是有用的

[EdmEntityTypeAttribute(NamespaceName="Irma2Model", Name="AccessProvider")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class AccessProvider : EntityObject
{
    /*****...... ******/
}

Found an answer, its not what you want to hear though:找到了答案,但不是您想听到的:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d2a07542-cb33-48ba-86ed-4fdc70fb3f1a http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d2a07542-cb33-48ba-86ed-4fdc70fb3f1a

"If you are using the default code generation for the EDMX file then the generated classes contain a series of attributes to help EF find which class to use for each entity type. EF currently has a restriction that POCO classes can't be loaded from an assembly that contains classes with the EF attributes. (Short answer is no, your classes need to be in separate projects). “如果您使用 EDMX 文件的默认代码生成,则生成的类包含一系列属性,以帮助 EF 找到用于每种实体类型的 class。EF 目前有一个限制,即 POCO 类不能从包含具有 EF 属性的类的程序集。(简短的回答是否定的,您的类需要在单独的项目中)。

This is a somewhat artificial restriction and something that we realize is painful and will try and remove in the future."这是一个有点人为的限制,我们意识到这是痛苦的,将来会尝试取消。”

So the workaround would be to split the classes into two different assemblies.因此,解决方法是将类拆分为两个不同的程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM