Wykryto sprzeczne zmiany w roli x relacji y

Mam wyjątek

Wykryto sprzeczne zmiany w roli x relacji y.

Za każdym razem, gdy dodaję swój element do mojego kontekstu

Database.MyEntitys.Add(MyEntity);

Klasa MyEntity zawiera tę właściwość:

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

Klasa DetailInfo jest bardzo prosta:

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

DatabaseContext jest również prosty:

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");
    }
}

Po stronie bazy danych tabela MyEntity ma klucz podstawowy do identyfikatora kolumny. DetailInfo ma również klucz podstawowy o nazwie ID. DetailInfo zawiera 2 FK, jeden do MyEntity i jeden do Detail, który jest innym podmiotem.

W problematycznym scenariuszu MyEntity jest nowy i ma nowy szczegół. Spodziewam się nowego wpisu dla MyEntity z nowym szczegółem i poprawną konfiguracją FK.

Edytować:

oto wstawka:

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);
}

questionAnswers(3)

yourAnswerToTheQuestion