简体   繁体   English

实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。”

[英]Entity Framework: “The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.”

I have this code (VS2010 ASP.NET MVC 3 with EF 4):我有这个代码(VS2010 ASP.NET MVC 3 和 EF 4):

Project project = new Project();
project.Number = number;
project.Name = name;
context.AddObject(project);

ProjectUser projectUser = new ProjectUser();
projectUser.User = user;
projectUser.Status = 1;
project.ProjectUsers.Add(projectUser);

context.SaveChanges(true);

It generates the following error (on the "project.ProjectUsers.Add(projectUser)" line)它生成以下错误(在“project.ProjectUsers.Add(projectUser)”行)

"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." “无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。”

I don't understand why cause, as far as I know, both objects are using the same ObjectContext (but I'm new to EF).我不明白为什么,据我所知,两个对象都使用相同的 ObjectContext(但我是 EF 新手)。

What am I doing wrong?我究竟做错了什么? Thanks for your help!谢谢你的帮助!

If your user variable is an entity type, and it is assigned to a different context, then you'd experience this problem.如果您的user变量是实体类型,并且分配给不同的上下文,那么您会遇到此问题。

I don't think the problem is between your Project and ProjectUser objects, only because your ProjectUser object isn't explicitly assigned to a context - I think by default it will go to the same context as the Project when you go to save it.我不认为问题出在您的ProjectProjectUser对象之间,只是因为您的ProjectUser object 没有明确分配给上下文 - 我认为默认情况下它会将 go 与Project相同的上下文 Z34D1F91FB2E514B8576FAB 保存1.7514B8576FAB

I believe you get this error only when you truly have two contexts and try to join them together.我相信只有当您真正拥有两个上下文并尝试将它们连接在一起时,您才会遇到此错误。

Just list you did for Project, you need to add the ProjectUser to the context.只需列出您为 Project 所做的事情,您需要将 ProjectUser 添加到上下文中。 So mimic the line:所以模仿这条线:

context.AddObject(project);

And instead make it而是让它

context.AddObject(projectUser);

And do that before you add it to the collection on project.并在将其添加到项目集合之前执行此操作。

I think a good way to avoid this problem is to implement your application database context as a singleton .我认为避免此问题的一个好方法是将您的application database上下文实现为singleton Here is a sample for you:这是给您的示例:

here your ApplicationDbContext.cs `java这里你的ApplicationDbContext.cs `java

public class ApplicationDbContext : DbContext, IDbContextFactory<DbContext>
{
    protected static ApplicationDbContext _instance { private set; get; }

    private ApplicationDbContext() : base("ApplicationDbContext")
    {
    }

    public DbContext Create()
    {
        return getInstance();
    }


    public static ApplicationDbContext getInstance()
    {
        if (_instance == null) {
            _instance = new ApplicationDbContext();
        }
        return _instance;
    }
}

` `

here your controller `java在这里你的 controller `java

private ApplicationDbContext _db;

public class HelloController : Controller
{
    _db = ApplicationDbContext.getInstance();
}

` `

so you can have the exact same instance no matter where you are in you app因此,无论您在应用程序中的哪个位置,都可以拥有完全相同的实例

I know this is old as dirt, but I spent several hours being frustrated by this unhelpful error today, and Babacar's answer finally pointed me in the right direction.我知道这已经过时了,但是今天我花了几个小时对这个无益的错误感到沮丧,而 Babacar 的回答终于为我指明了正确的方向。

With Ninject managing dependencies, I needed to specify使用 Ninject 管理依赖项,我需要指定

kernel.Bind<Context>().ToSelf().InSingletonScope();

before my EF calls between the UserManager and various controllers started behaving properly.在我的 UserManager 和各种控制器之间的 EF 调用开始正常运行之前。

You shouldn't need this line at all:你根本不需要这条线:

project.ProjectUsers.Add(projectUser);

Just adding the project should be sufficient, because you're setting the relationship.只需添加项目就足够了,因为您正在设置关系。

context.AddObject(project);

暂无
暂无

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

相关问题 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象Entity Framework - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework 实体框架v4 - 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects “无法定义两个对象之间的关系,因为它们被附加到不同的ObjectContext对象。” -错误 - 'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error 无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 引发异常:因为两个对象附​​加到不同的ObjectContext对象,所以无法定义它们之间的关系 - Exception thrown: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们已附加到不同的ObjectContext对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义关系,因为它们已附加到不同的ObjectContext对象 - Relationship cannot be defined because they are attached to different ObjectContext objects 无法找出哪些两个对象附​​加到不同的ObjectContext对象 - Cannot figure out which two objects are attached to different ObjectContext objects 实体框架 - “两个对象之间的关系无法定义”错误,但我认为我使用的是相同的上下文 - Entity Framework - “The relationship between the two objects cannot be defined” error, but I think I'm using the same context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM