Database operation expected to affect 1 row(s) but actually affected 50(db sequence next number) row(s)

梦想的初衷 提交于 2019-12-11 14:39:20

问题


I use entity framework 2.2.6. I have AddOrUpdate method. When client try to operate(insert or update) on entity I check if entity exist on db by unique values and I set old data id value to new data id value and I set it as updated. If not exist I set it as insert

public void AddOrUpdate(TEntity entity, Expression<Func<TEntity, bool>> predicate)
        {
            var oldEntity = Get(predicate);
            DetachEntry(oldEntity);

            if (oldEntity != null)
            {
                var idProperty = entity.GetType().GetProperties().First(x => x.Name == "Id");
                idProperty.SetValue(entity, idProperty.GetValue(oldEntity, null));

                Update(entity);
            }
            else
                Add(entity);
        }

When I try simple entity It works fine. But when I use very complex type I get following exception.

Database operation expected to affect 1 row(s) but actually affected 50 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

My complex type is like this.

public class FamilyMemberInfo 
    {

        public int Id { get; set; }

        public int AppealId { get; set; }

        public int MemberId { get; set; }

        public FamilyMember FamilyMember { get; set; }

        public Appeal Appeal { get; set; }

        public ICollection<FamilyMemberContact> Contacts { get; set; }

        public ICollection<FamilyMemberCrime> Crimes { get; set; }

        public ICollection<FamilyMemberDisease> Diseases { get; set; }

        public ICollection<FamilyMemberPaymentLiability> PaymentLiabilities { get; set; }


    }

After call AddOrUpdate method I look ChangeTraker every thing seem ok. And it generated very good query.

I can not find exception reason. I see familar errors on stakoverflow. But every solitions not work for me.

Edit

When I comment two child entities(it is not important which ones) everythink works fine

来源:https://stackoverflow.com/questions/58503808/database-operation-expected-to-affect-1-rows-but-actually-affected-50db-seque

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!