已检测到的关系y的作用X产生冲突的变化 [英] Conflicting changes to the role x of the relationship y have been detected

查看:489
本文介绍了已检测到的关系y的作用X产生冲突的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有例外


  

到关系y的作用X产生冲突的变化被检测到。


我每次我的实体添加到我的上下文

  Database.MyEntitys.Add(myEntity所);

类myEntity所包含此属性:

  public虚拟的ICollection< D​​etailInfo>集团{搞定;组; }

该DetailInfo类是pretty简单:

 公共类DetailInfo:BaseEntity {
    公共虚拟详细细节{搞定;组; }
    公共小数总{搞定;组; }
    公共虚拟myEntity所myEntity所获得{;组; }
}

该DatabaseContext也很简单:

 公共类MyEntityConfiguration:EntityTypeConfiguration<&myEntity所GT; {
    公共MyEntityConfiguration(){
        的hasMany(E => e.Group).WithRequired(S = GT; s.MyEntity).WillCascadeOnDelete(真);
    }
}公共类DetailInfoConfiguration:EntityTypeConfiguration< D​​etailInfo> {
    公共DetailInfoConfiguration(){
        HasRequired(X => x.MyEntity).WithMany(S => s.Group);
        HasRequired(X => x.Detail);
        HasKey(S => s.ID);
        ToTable(DetailInfo);
    }
}

在数据库端表myEntity所拥有主键列ID。该DetailInfo也有一个名为ID的主键。 DetailInfo包含2 FK,一个myEntity所和一个细节是另一个实体。

在有问题的情况下,myEntity所是新建了一个新的细节。我期待着为myEntity所用新的详细的新条目,并正确让所有FK设置。

编辑:

这里是插入:

 公共虚拟INT插入(myEntity所myEntity所){    如果(myEntity.Group = NULL&放大器;!&安培; myEntity.Group.Count()== 0){
        myEntity.Group = NULL;
    }    如果(myEntity.Group!= NULL){
        的foreach(在myEntity.Group变种G)
        {
         如果(g.PropertyOneToOne!= NULL){
                如果(g.PropertyOneToOne.ID == 0){
                    myEntity.PropertyOneToOne = NULL;
                }
                其他{
                    如果(Database.PropertyOneToOnes.Local.Any(E =>!e.ID == g.PropertyOneToOne.ID)){
                        Database.PropertyOneToOnes.Attach(g.PropertyOneToOne);
                    }
                    myEntity.PropertyOneToOne = Database.PropertyOneToOnes.Local.Single(E => e.ID == g.PropertyOneToOne.ID);
                }
            }
            其他{
                myEntity.PropertyOneToOne = NULL;
            }
        }
    }
    Database.MyEntitys.Add(myEntity所);
}


解决方案

的问题是这样的:

myEntity所拥有的0一个ID,因为它是一个新的myEntity所。
该集团也是新的,并包含对myEntity所引用。

因此​​, myEntity所含有包括引用回组列表 myEntity所

问题是, MyEntity.Group.MyEntity 似乎是新的,不一样为 myEntity所 。当添加myEntity所要的范围内,它发现一个冲突

要解决我设置的问题 Group.MyEntity 为NULL,并保留 myEntity所。保存时, myEntity所引用(ID)设置成组表。

我不知道它是做最彻底的方法,但问题终于解决了。

I am having the exception

Conflicting changes to the role x of the relationship y have been detected.

Every time I add my entity to my context

Database.MyEntitys.Add(MyEntity);

The class MyEntity contain this property :

public virtual ICollection<DetailInfo> Group { get; set; }

The DetailInfo class is pretty simple:

public class DetailInfo:BaseEntity {
    public virtual Detail Detail { get; set; }
    public decimal Total { get; set; }
    public virtual MyEntity MyEntity { get; set; }
}

The DatabaseContext is also simple:

public class MyEntityConfiguration : EntityTypeConfiguration<MyEntity> {
    public MyEntityConfiguration() {
        HasMany(e => e.Group).WithRequired(s => s.MyEntity).WillCascadeOnDelete(true);
    }
}

public class DetailInfoConfiguration : EntityTypeConfiguration<DetailInfo> {
    public DetailInfoConfiguration() {
        HasRequired(x => x.MyEntity).WithMany(s => s.Group);
        HasRequired(x => x.Detail);
        HasKey(s => s.ID);
        ToTable("DetailInfo");
    }
}

On the Database side the table MyEntity has a primary key to the column ID. The DetailInfo has also a primary key called ID. DetailInfo contains 2 FK, one to MyEntity and one to Detail which is another entity.

In the problematic scenario, the MyEntity is new has a new Detail. I am expecting a new entry for MyEntity with a new Detail and having all FK correctly setup.

Edit:

here is the Insert:

public virtual int Insert(MyEntity myEntity) {

    if (myEntity.Group != null && myEntity.Group.Count() == 0) {
        myEntity.Group = null; 
    }

    if (myEntity.Group != null) {
        foreach (var g in myEntity.Group)
        {
         if (g.PropertyOneToOne != null) {
                if (g.PropertyOneToOne.ID == 0) {
                    myEntity.PropertyOneToOne = null;
                }
                else {
                    if (!Database.PropertyOneToOnes.Local.Any(e => e.ID == g.PropertyOneToOne.ID)) {
                        Database.PropertyOneToOnes.Attach(g.PropertyOneToOne);
                    }
                    myEntity.PropertyOneToOne = Database.PropertyOneToOnes.Local.Single(e => e.ID == g.PropertyOneToOne.ID);
                }
            }
            else {
                myEntity.PropertyOneToOne = null;
            }
        }
    }
    Database.MyEntitys.Add(myEntity);
}

解决方案

The problem is this one:

MyEntity has an ID of 0 since it's a new MyEntity. The Group is also new and contain a reference to MyEntity.

So, MyEntity contains a list of Group which contain a reference back to MyEntity.

The problem is that MyEntity.Group.MyEntity seems to be "new and not the same" as MyEntity. When adding MyEntity to the context, it found a conflict.

To solve the problem I set Group.MyEntity to NULL and keep the reference of Group inside the list of MyEntity. When saving, MyEntity reference (ID) is set into the Group table.

I am not sure it's the cleanest way to do it, but the problem is finally solved.

这篇关于已检测到的关系y的作用X产生冲突的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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