Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 213
Saeid Avatar asked Mar 07 '23 01:03

Saeid


1 Answers

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.

like image 158
Saeid Avatar answered May 07 '23 22:05

Saeid