gpt4 book ai didi

asp.net-core - Automapper : The instance of entity type cannot be tracked because another instance with the key is already being tracked 使用异常

转载 作者:行者123 更新时间:2023-12-04 01:33:58 27 4
gpt4 key购买 nike

我将 ASP.NET Core 与 EFCore 2.0.3 和 Automapper 6.2.2 一起使用

这是我的模型:

    public class StudentClass
{
public int Id { get; set; }
public int ClassId { get; set; }
public Class Class { get; set; }
public int ProfId { get; set; }
public Professor Prof { get; set; }
public string Description { get; set; }
public DateTimeOffset LastUpdateTime { get; set; }
public DateTimeOffset CreateTime { get; set; }
}

和我的实体
    public class StudentClassEntity
{
public int Id { get; set; }

public int ClassId { get; set; }
[ForeignKey("ClassId")]
public virtual Class Class { get; set; }
public int ProfId { get; set; }
[ForeignKey("ProfId")]
public Professor Prof { get; set; }
public string Description { get; set; }
public DateTimeOffset LastUpdateTime { get; set; }
public DateTimeOffset CreateTime { get; set; }
public virtual List<Task> Tasks { get; set; }
}

我尝试更新 StudentClass所以这是我的示例方法:
        public void Update()
{

var studentclass = new StudentClass();
studentclass.Id = 7;
studentclass.CreateTime = System.DateTimeOffset.MinValue;
studentclass.Description = $"new desc - {System.DateTime.Now.Millisecond}";
studentclass.ProfId = 5;
studentclass.ClassId = 7;


var entity = _context.StudentClasses.FirstOrDefault(x => x.Id == studentclass.Id);

if (entity != null)
{
entity = _mapper.Map<StudentClassEntity>(studentclass);
_context.StudentClasses.Update(entity);
_context.SaveChanges();
}
}

还有我的 Automapper map :
 CreateMap<StudentClass, StudentClassEntity>()
.ForMember(m => m.Description, o => o.MapFrom(x => x.Description))
.ForMember(m => m.LastUpdateTime, o => o.MapFrom(f => DateTimeOffset.Now))
.ForAllOtherMembers(m => m.UseDestinationValue());

所以我得到了异常(exception):
The instance of entity type cannot be tracked because another instance with the key value "Id:7" is already being tracked.

但是,如果我不使用 Automapper,而是手动映射:
if (entity != null)
{
//entity = _mapper.Map<StudentClassEntity>(studentclass);
entity.Description = studentclass.Description;
entity.LastUpdateTime = DateTimeOffset.Now;

_context.StudentClasses.Update(entity);
_context.SaveChanges();
}

它将无一异常(exception)地更新数据库。问题出在哪儿?我错过了什么吗?如何在 Automapper 中创建 Map 以毫无异常(exception)地更新实体?

最佳答案

我发现如果我使用

entity = _mapper.Map<StudentClassEntity>(studentclass);

Automapper 将为实体创建新对象,显然它在上下文中不是相同的引用。但如果我用
_mapper.Map(studentclass, entity);

Automapper 不创建新实例,实体与上下文中的引用相同。

关于asp.net-core - Automapper : The instance of entity type cannot be tracked because another instance with the key is already being tracked 使用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com