使用AutoMapper的“相同类型的实体已经具有相同的主键值"错误 [英] 'entity of the same type already has the same primary key value' error using AutoMapper

查看:177
本文介绍了使用AutoMapper的“相同类型的实体已经具有相同的主键值"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用AutoMapper时,发生此错误:

When I use AutoMapper, this Error occured:

附加类型为'MyProject.DAL.User'的实体失败,因为相同类型的另一个实体已经具有相同的主键值.如果图形中的任何实体具有冲突的键值,则使用附加"方法或将实体的状态设置为不变"或修改"时,可能会发生这种情况.这可能是因为某些实体是新实体,尚未收到数据库生成的键值.在这种情况下,请使用添加"方法或已添加"实体状态来跟踪图形,然后根据需要将非新实体的状态设置为未更改"或已修改".

Attaching an entity of type 'MyProject.DAL.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

当我从数据库中检索UserModel时,我想将其映射到UserModel.我在UI中更改UserModel属性,然后将其再次映射到User并更新它. 我的代码在这里:

I want to map User to UserModel when I retrive it from database. I change UserModel properties in UI and then map it again to User and Update it. My code is here:

public UserModel GetUserByUserId(int id)
    {
        var user = db.Users.Where(p => p.UserId == id).FirstOrDefault();
        var userModel = Mapper.Map<UserModel>(user);
        return userModel;
    }

public void Update(UserModel userModel)
    {
        var user = Mapper.Map<User>(userModel);
        db.Entry(user).State = EntityState.Modified;
        db.SaveChanges();
    }

但是如果我不使用自动映射器并编写类似下面的代码的代码,它将正常工作.

but if I don't Use auto mapper and write something like below code, it work correctly.

public void Update(UserModel userModel)
    {
        updatingUser.Email = userModel.Email;
        updatingUser.FirstName = userModel.FirstName;
        updatingUser.ModifiedDate = DateTime.Now;
        updatingUser.LastName = userModel.LastName;
        updatingUser.Password = userModel.Password;
        updatingUser.UserName = userModel.UserName;

        db.Entry(updatingUser).State = EntityState.Modified;
        db.SaveChanges();
    }

我该怎么办:

推荐答案

这可能只是我不知道某些功能,但是您的update函数对我来说看起来很时髦.我看不到它将如何将新的user与数据库中的现有user相关联.

This might just be me being unaware of some features, but your update function looks funky to me. I don't see how it would associate your new user with the existing one in the db.

这就是我的处理方式.

public void Update(UserModel userModel)
{
    var user = db.Users.Find(userModel.UserId);
    Mapper.Map(userModel, user);
    db.SaveChanges();
}

或者,如果您愿意像第二个update函数一样执行操作

or, if you prefer to do it like your second update function does

public void Update(UserModel userModel)
{
    Mapper.Map(userModel, updatingUser);
    db.Entry(updatingUser).State = EntityState.Modified;
    db.SaveChanges();
}

这篇关于使用AutoMapper的“相同类型的实体已经具有相同的主键值"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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