实体框架,执行顺序和变更集

时间:2015-11-04 20:28:31

标签: c# entity-framework-5

我有以下实体:

public partial class MyEntity
{
        List<MyEntity> _Items = new List<MyEntity>();

        [Key]
        public int MyEntityId { get; set; }
        public int ParentId { get; set; }

        [Include, Composition, Association("MyEntity_MyEntity", "MyEntityId", "ParentId")]
        public List<MyEntity> Items
        {
            get { return _Items; }
            set { _Items = value; }
        }
}

当我删除此实体列表中的项目然后向其添加相同实体时,我收到有关DB中重复记录的错误。

我已经覆盖了Submit这样的方法:

public override bool Submit(ChangeSet changeSet)
{
            var submit1 = false;

            if (changeSet.ChangeSetEntries.Any(item => item.Operation == DomainOperation.Delete))
            {
                var deletedList = new ChangeSet(changeSet.ChangeSetEntries.Where(item => item.Operation == DomainOperation.Delete));
                submit1 = base.Submit(deletedList);
            }

            var submit2 = false;
            if (changeSet.ChangeSetEntries.Any(item => item.Operation == DomainOperation.Insert))
            {
                var insertedList = new ChangeSet(changeSet.ChangeSetEntries.Where(item => item.Operation == DomainOperation.Insert));
                submit2 = base.Submit(insertedList);
            }

            var submit3 = false;
            if (changeSet.ChangeSetEntries.Any(item => item.Operation == DomainOperation.Update))
            {
                var updatedList = new ChangeSet(changeSet.ChangeSetEntries.Where(item => item.Operation == DomainOperation.Update));
                submit3 = base.Submit(updatedList);
            }

            return submit1 && submit2 && submit3;
}

但现在,当它试图获取插入列表时,我收到以下错误:

  

无效的ChangeSet:每个条目的ID必须是唯一的

你能告诉我为什么会这样吗?

感谢。

0 个答案:

没有答案