简体   繁体   English

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

[英]Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

This my first question on SO, although I have been using it as a resource for a long time. 这是关于SO的第一个问题,尽管我长期以来一直将它作为资源使用。

First things first, I know there are a lot of questions on this topic and I think I have tried all the various solutions to no avail but it maybe I am missing something fundamental. 首先,我知道有很多关于这个主题的问题,我想我已经尝试了所有各种解决方案但没有用,但也许我错过了一些基本的东西。

I am using EF4 on an asp.net MVC web application, not sure there is anything else you need to know but shout if so. 我在asp.net MVC网络应用程序上使用EF4,不确定还有什么你需要知道但是如果是这样的话就大喊大叫。

So far as I can tell I am only using one context "db" and I am not using "using" statements so I can't see that the context is being disposed of, at least not on purpose but yet I keep hitting the same error above. 据我所知,我只使用一个上下文“db”而我没有使用“using”语句,所以我看不到上下文被处理掉了,至少不是故意的,但我还是一直在打上面的错误。

The situation is this I have an action result that creates a batch of entries "ReportSections" connected to the main model "Report" based on the "Claims" which are connected to the Report through a ClaimGroup (see below). 情况是我有一个动作结果,它创建了一组条目“ReportSections”连接到主模型“Report”,基于通过ClaimGroup连接到Report的“Claims”(见下文)。

My (simplified) classes therefore look like this; 因此,我的(简化)课程看起来像这样;

public partial class Claim
{
    public Claim()
    {
        this.ReportSections = new HashSet<ReportSection>();
        this.Reports = new HashSet<Reports>();
    }

    public int id {get; set;}
    //other stuff

    public virtual ICollection<Reports> Reports {get; set;}
    public virtual ICollection<ReportSections> ReportSections {get; set;}
}

public partial class ClaimGroup
{
    public ClaimGroup()
    {
        this.Claims = new HashSet<Claims>();
    }

    public int id {get; set;}
    //other stuff

    public virtual ICollection<Claims> Claims {get; set;}
    public virtual ICollection<ReportSections> ReportSections {get; set;}
}

public partial class Report
{
    public Report()
    {
        this.ReportSections = new HashSet<ReportSection>();
    }

    public int id {get; set;}
    public int ClaimGroupId {get; set;}
    //other stuff

    public virtual ICollection<ReportSections> ReportSections {get; set;}
    public virtual ClaimGroup ClaimGroup {get; set;}
}

public partial class ReportSection
{
    public int id {get; set;}
    public int ClaimId {get; set;}
    public int ReportId {get; set;}
    public int Position {get; set;}
    //other stuff

    public virtual Report Report {get; set;}
    public virtual Claim Claim {get; set;}
}

[For the avoidance of doubt I am happy that the many-to-many relationship between Claims and ClaimGroups is working ok as it functions in other areas of the site.] [为避免疑问,我很高兴声明和ClaimGroups之间的多对多关系正常工作,因为它在网站的其他区域运行。

My Controller then has the following; 我的控制器有以下内容;

public ActionResult BuildSections(int id)
{
    Report r = db.Reports.Find(id);
    int i = 0;
    foreach(Claim c in r.ClaimGroup.Claims)
    {
        r.ReportSections.Add(new ReportSection { Claim = c, Position = i});
        i++;
    }
    db.SaveChanges();
}

It is really irritating as it doesn't seem complex but I am clearly doing something wrong. 它真的很烦人,因为它似乎并不复杂,但我显然做错了什么。

Thanks in advance. 提前致谢。

Sy SY

EDIT 编辑

The error triggers on saving back to the database so in the example here it is on 保存回数据库时会触发错误,因此在此示例中它处于打开状态

db.SaveChanges();  

However, in my various attempts to find a solution I have also tried 但是,在我尝试寻找解决方案的各种尝试中,我也尝试过

db.ReportSections.Add(new ReportSection {...}); 

and there the error occurred on that line. 那条线上发生了错误。

Thank you to those who took the time to give me some pointers. 感谢那些花时间给我一些指示的人。 I found the issue and thought I would post it in case it helps anyone else who makes such a school boy error. 我找到了这个问题,并认为我会发布它,以防它帮助那些造成这样一个学校男孩错误的人。

My Claim class was made up of two partial classes in one there were some calculated fields and one of these used another context. 我的Claim类由两个部分类组成,其中一个有一些计算字段,其中一个使用另一个上下文。 It was that which the error was referring to not anything in the ActionResult above but of course it couldn't resolve the two when it came to saving the changes. 错误所指的是上面的ActionResult中没有任何内容,但当然在保存更改时无法解决这两个问题。

Thanks again 再次感谢

Sy SY

暂无
暂无

声明:本站的技术帖子网页,遵循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 实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - Entity Framework: “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对象。” -错误 - 'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error 引发异常:因为两个对象附​​加到不同的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