Exception in use Automapper: The instance of entity type cannot be tracked because another instance with the key is already being tracked

asp.net-core automapper entity-framework-core

Question

I use ASP.NET Core with EFCore 2.0.3 and Automapper 6.2.2

Here is my model:

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

And My Entity

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

And I tried to update StudentClass So here is my sample method:

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

And my 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());

So I got the exception:

The instance of entity type cannot be tracked because another instance with the key value "Id:7" is already being tracked.

But if instead of using Automapper I just map manually:

if (entity != null)
{
     //entity = _mapper.Map<StudentClassEntity>(studentclass);
    entity.Description = studentclass.Description;
    entity.LastUpdateTime = DateTimeOffset.Now;

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

It would be updated DB without exception. Where is the problem? Did I missed anythings? How create Map in Automapper to update entity without exception?

1
2
6/8/2018 2:34:46 PM

Accepted Answer

I found that if I use the

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

The Automapper will create the new object for entity and obviously it's not the same reference in context. But if I used

_mapper.Map(studentclass, entity);

The Automapper not create the new instance and entity is the same reference as in context.

3
6/8/2018 2:36:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow