Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing an application in MVC5 that has its own data access layer. In data access layer I have tried mapping object for Add/Update operation. I am using the
C#
AutoMapper v4.0.30319


Please find below image for see my code.

code image

What I have tried:

1st, I have tried: I did create a another
patientModelDAL.cs
model class in my Data Access Layer and tried to map the model class to Entity Framework
patient.cs
model class.

C#
Patient patient = t_Context.Patients.Where(p => p.idPatient == t_Entity.idPatient).FirstOrDefault();
var config = new MapperConfiguration(cfg =>
                   {
                       cfg.CreateMap<patientModelDAL, Patient>();
                   });
                   IMapper iMapper = config.CreateMapper();
                   patient = iMapper.Map<patientModelDAL, Patient>(t_Entity);
t_Context.Entry(patient).State = EntityState.Modified;
t_Context.SaveChanges();


Then, the error raised:

Attaching an entity of type 'EMRClinic.DAL.App_Data.Patient' 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.

How can I use AutoMapper v4.0.30319 while updating Entity model objects?
Posted
Updated 12-Jul-21 0:52am
Comments
Richard Deeming 12-Jul-21 6:45am    
"AutoMapper v4.0.30319"
Why such an old version? The current version (v10.1.1) has been out for nine months. You seem to be using a version from some time in 2015.
Member 12955507 12-Jul-21 12:45pm    
I am using VS2019. There is showing "Version 10.0.0.0" and "Runtime Version v4.0.30319". I updated automapper still showing like this. I don't know.
Richard Deeming 12-Jul-21 12:47pm    
"Runtime version" means the version of the .NET Framework, not the version of the AutoMapper assembly.

It looks like you're using v10, which is fairly recent.
Member 12955507 12-Jul-21 22:22pm    
Thank for your valuable knowledge share me. Feeling Love! And I am going to apply your solution logic idea for handle the mapper while Add/Update my tables.

1 solution

The problem is you are loading the entity with the specified ID, then creating a new instance of the class with the same ID, and attaching that new instance to the context.

Instead, you should be updating the existing entity instance:
C#
Patient patient = t_Context.Patients.FirstOrDefault(p => p.idPatient == t_Entity.idPatient);
if (patient == null)
{
    patient = new Patient();
    t_Context.Patients.Add(patient);
}

iMapper.Map(t_Entity, patient);
t_Context.SaveChanges();

NB: For performance reasons, you should be configuring your mappings once, when your application starts up.
Getting Started Guide — AutoMapper documentation[^]
 
Share this answer
 
Comments
Member 12955507 13-Jul-21 6:16am    
Thank you dear! you saved my time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900