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

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?

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.

Related

How do I insert nested objects correctly in Ef

I had a problem trying to add object from dto.
I have a DTO like this
public class CustomerCourseSessionDto : IDto
{
public int Id { get; set; }
public int? ConnectorId { get; set; }
public List<SomeObject>? SomeObject { get; set; }
public List<OtherObject>? OtherObject { get; set; }
}
and I'm trying to insert this DTO sent from the UI. I'm using a loop(foreach) for this, but it didn't feel right to me.
Maybe entityframework suggests a method for this, idk...
Other objects are like this
public class SomeObject: IEntity
{
public int Id { get; set; }
public int? ConnectorId { get; set; }
}
public class OtherObject: IEntity
{
public int Id { get; set; }
public int? ConnectorId { get; set; }
}
Thanks for the suggestions.
Here is where I use the loop:
[TransactionScopeAspect]
public IResult AddWithDto(CustomerCourseSessionDto courseSessionDto)
{
foreach (var item in courseSessionDto.Participants)
{
_someObjectService.Add(item);
}
foreach (var item in courseSessionDto.Lessons)
{
_otherObjectService.Add(item);
}
_customerCourseSessionDal.Add(new CustomerCourseSession
{
Id = courseSessionDto.Id,
CustomerCourseId = courseSessionDto.CustomerCourseId,
});
return SuccessResult with message ;
}
You should design your entities to have a link to each table using foreign keys.
public class CustomerCourseSession
{
public int Id { get; set; } // Primary Key
public string Name { get; set; }
public virtual ICollection<CourseParticipant> Participants { get; set; }
public virtual ICollection<CourseLesson> Lessons { get; set; }
}
public class CourseParticipant
{
public int Id { get; set; }
public int CustomerCourseSessionId { get; set; } // Foreign Key from CustomerCourseSession
// other fields here
public virtual CustomerCourseSession CustomerCourseSession { get; set;}
}
public class CourseLesson
{
public int Id { get; set; }
public int CustomerCourseSessionId { get; set; } // Foreign Key from CustomerCourseSession
// other fields here
public virtual CustomerCourseSession CustomerCourseSession { get; set;}
}
Then you can just call:
var entity = MapDtoToEntity(courseSessionDto);
_dbContext.CustomerCourseSessions.Add(entity);
_dbContext.SaveChanges();

How to get the discriminator from the id of an entitie?

I would like to now wether my id is an invoice or an individualinvoice
individualinvoice.cs
public class IndividualInvoice : Invoice {
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
invoice.cs
public class Invoice {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[Required]
public string Company { get; set; }
[Required]
public string Address { get; set; }
[Required]
public int HouseNumber { get; set; }
[Required]
public string Zipcode { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string VATNumber { get; set; }
public Customer Customer { get; set; }
[ForeignKey("Customer")]
[Required]
public string CustomerId { get; set; }
}
gingsengdbcontext.cs
public class GingsengDbContext : IdentityDbContext<GingsengUser> {
public DbSet<Gingseng> Gingsengs { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<IndividualInvoice> IndividualInvoices { get; set; }
public GingsengDbContext(DbContextOptions<GingsengDbContext> options) : base(options)
{
}
}
And here is my controller where i would like to know from the id if the id corresponds to an individialinvoice or just an invoice? is there any cleaner way than to use singleordefault?
public class InvoicesController : Controller {
private readonly GingsengDbContext context;
private readonly IMapper mapper;
public InvoicesController(GingsengDbContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetInvoice(string id) {
}
}
Well, the only clean way which works with all EF Core supported inheritance models (currently TPH and TPT) is to use C# is operator. However the classes must not inherit other non abstract class from the same hierarchy like in your example, because IndividualInvoice is a Invoice, hence will be included in DbSet<Invoice> and any query (OfType etc.) which checking for Invoice.
So you can check just for final classes, e.g.
bool isIndividualInvoice = await context.Invoices
.AnyAsync(e => e.Id == id && e is IndividualInvoice);
which btw is the same as
bool isIndividualInvoice = await context.IndividualInvoices
.AnyAsync(e => e.Id == id);
and similar (using Set<IndividualInvoice>() or Set<Invoice>().OfType<IndividualInvoice>).
Another not so clean option which works only for TPH is to retrieve the discriminator property value directly. You have to know its name and type (the defaults are "Discriminator" and string) and use the special EF.Property method similar to this:
var type = await context.Invoices
.Where(e => e.Id == id)
.Select(e => EF.Property<string>(e, "Discriminator")) // <--
.FirstOrDefaultAsync();
// here type will be ether null, "Invoice" or "IndividualInvoice"

Problem with mapping two objects (with lists)

I am looking for solution my issue... Probably my Shifts class cannot be mapped.
I have entity class Worker:
public class Worker
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(200)]
public string PhotoFilePath { get; set; }
public Workplace Workplace { get; set; }
public int WorkplaceId { get; set; }
public List<Service> Services { get; set; }
public List<Shift> Shifts { get; set; }
public IEnumerable<Worker> ToList()
{
throw new NotImplementedException();
}
}
And model WorkerModel:
public int Id { get; set; }
[Required]
[DisplayName("Imię")]
public string Name { get; set; }
[DisplayName("Nazwisko")]
public string LastName { get; set; }
[Display(Name = "Zdjęcie")]
public IFormFile Photo { get; set; }
public string PhotoFilePath { get; set; }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int WorkplaceId { get; set; }
public List<ServiceModel> Services { get; set; }
public List<ShiftModel> Shifts { get; set; }
}
My default mapper profile:
//Mapping workers
CreateMap<Worker, WorkerModel>();
CreateMap<WorkerModel, Worker>();
And when I try map model to entity class in my action:
Worker worker = _mapper.Map<Worker>(model);
I get an issue:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
This is caused by different mapping types. Take the property Service as an example.
The resource is a type of Service.
But the destination is a type of ServiceModel.
So, they need to be converted. Here is a demo.
I create the Service and ServiceModel according to your model.
public class Service
{
public int serviceID { get; set; }
public string myservice { get; set; }
}
public class ServiceModel
{
public int serviceID { get; set; }
public string myservice { get; set; }
}
This is mapping relationship.
public class AutomapProfile : Profile
{
public AutomapProfile()
{
CreateMap<Worker, WorkerModel>();
CreateMap<WorkerModel, Worker>()
.ForMember(m => m.Services, x => x.MapFrom(y => y.Services.Select(a=>
new Service
{
serviceID=a.serviceID,
myservice=a.myservice
})));
}
}
This is the mapping method.
public IActionResult Index()
{
var model = new WorkerModel
{
Id=1,
Name="names",
//...
Services = new List<ServiceModel>
{
new ServiceModel{ serviceID=1, myservice="service1"},
new ServiceModel{ serviceID=2, myservice="service2"},
},
//...
};
Worker worker = _mapper.Map<Worker>(model);
return Ok(worker);
}
Result.

Automapper ConstructUsing not working as expected

I am using automapper in my asp.net core project and it's my first time with that library. The data flow is as follows: Model->DomainModel->ViewModel. Automapper is used for mapping between those. I have problems using ConstructUsing. It seems to me it is not working.
Part of the data profile class:
CreateMap<ClinicD, ClinicViewModel>()
.ConstructUsing(x => new ClinicViewModel
{
Active = x.Active,
CooperationStart = x.CooperationStart,
CooperationEnd = x.CooperationEnd,
Id = x.Id,
Name = x.Name,
AddressId = x.Address.Id,
FlatNo = x.Address.FlatNo,
City = x.Address.City,
HouseNumber = x.Address.HouseNumber,
Street = x.Address.Street,
Postcode = x.Address.Postcode
})
.ForMember(x => x.ChosenSpecialities, opt => opt.Ignore())
.ForMember(x => x.Specialities, opt => opt.Ignore());
public class ClinicViewModel : CooperationSpotViewModel
{
private IEnumerable<int> _chosenSpecialities;
public IEnumerable<SelectListItem> Specialities
{
get;set;
}
public IEnumerable<int> ChosenSpecialities
{
get
{
if (_chosenSpecialities == null)
_chosenSpecialities = new List<int>();
return _chosenSpecialities;
}
set
{
if (value != null)
_chosenSpecialities = value;
}
}
}
public abstract class CooperationSpotViewModel : BaseViewModel
{
public int? Id { get; set; }
public string Name { get; set; }
public DateTime? CooperationStart { get; set; }
public DateTime? CooperationEnd { get; set; }
public bool? Active { get; set; }
public string PhoneNumber { get; set; }
public int? AddressId { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string FlatNo { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
}
public class ClinicD : CooperationSpotD
{
public IEnumerable<SpecialityD> Specialities
{
get;set;
}
}
public abstract class CooperationSpotD
{
public int? Id { get; set; }
public string Name { get; set; }
public DateTime? CooperationStart { get; set; }
public DateTime? CooperationEnd { get; set; }
public bool? Active { get; set; }
public string PhoneNumber { get; set; }
public AddressD Address { get; set; }
}
A similar issue occurs for me in several spots, so I am guessing, I must be doing something basic wrong. The exception that occurs:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
ClinicD -> ClinicViewModel (Destination member list)
SeeingEyeDog.BusinessLogic.Models.ClinicD -> SeeingEyeDog.Models.ClinicViewModel (Destination member list)
Unmapped properties:
Street
HouseNumber
FlatNo
City
Postcode

Entity Framework 6 - child property data not loading

The ManagingAgent child property on the Complex entity is not being loaded with data.... possibly the result of too much mulled wine.
I have logged the SQL on the database calls and the SQL is returning the correct data.
LazyLoading is disabled.
public ApplicationDbContext()
: base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = false;
}
Aggregate Root
public class Complex
{
public Complex()
{
Forums = new List<Forum>();
ManagingAgent = new ManagingAgent();
}
[Key]
public int ComplexId { get; set; }
[Required]
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public int? PostCodeId { get; set; }
public PostCode PostCode { get; set; }
public int? LocationId { get; set; }
public Location Location { get; set; }
public int? CountyId { get; set; }
public County County { get; set; }
public int? ManagingAgentId { get; set; }
public ManagingAgent ManagingAgent { get; set; }
public int? CountOfUnits { get; set; }
public List<Forum> Forums { get; set; }
}
Attempt 1. using Include...
public List<Complex> GetComplexesByUserId(Guid userId)
{
using (var db = new ApplicationDbContext())
{
db.Database.Log = Logger;
var complexIds = db.UserApartments.Where(r => r.UserId == userId)
.Select(c => c.ComplexId).ToList();
return db.Complexes.Where(c => complexIds.Contains(c.ComplexId))
.Include(m => m.ManagingAgent).ToList();
}
}
Attempt 2 - explicitly loading ..same result (SQL returns data correctly but ManagingAgent isn't populated)
public List<Complex> GetComplexesByUserId(Guid userId)
{
using (var db = new ApplicationDbContext())
{
db.Database.Log = Logger;
var complexIds = db.UserApartments.Where(r => r.UserId == userId)
.Select(c => c.ComplexId).ToList();
var list = new List<Complex>();
foreach (var id in complexIds)
{
var complex = db.Complexes.Find(id);
db.Entry(complex).Reference(m => m.ManagingAgent).Load();
list.Add(complex);
}
return list;
}
}
So, to force the load I am doing this.... not good..
foreach (var id in complexIds)
{
var complex = db.Complexes.Find(id);
var managingAgent = db.ManagingAgents.Find(complex.ManagingAgentId);
complex.ManagingAgent = managingAgent;
list.Add(complex);
}
Remove this line...
ManagingAgent = new ManagingAgent();
...from the constructor of the Complex entity. Then it will work. (Generally don't instantiate reference navigation properties in an entity default constructor. EF calls this constructor via reflection when it materializes the entity and "gets confused" if the navigation property already has a reference. I can't explain the "gets confused" better since I don't know the exact mechanism of object materialization with related entities, but the effect is that the loaded child column values are ignored because there is already an instantiated child entity, but just with the useless default values from the ManagingAgent constructor.)