I am trying to update an Entity EF Core record like this:
public class Customer
{
public int Id { get; set; }
public int Name { get; set; }
...
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public int ZoneId { get; set; }
public virtual Zone Zone { get; set; }
public ICollection Services { get; set; }
public virtual ICollection Invoices { get; set; }
}
When I call Context.Update() or Context.Add(), also update Zone and Country entities. I don't want that the virtual properties update. I am trying, with reflection, get the virtual properties for indicate that Entry(virtualProperty).State = EntityState.Detached, but I can not. This is the code that I am trying.
Type typeOfTEntity = typeof(TEntity);
foreach (var property in typeOfTEntity.GetProperties())
{
if (property.GetGetMethod().IsVirtual)
{
foreach (var member in context.Context.Entry(CurrentItem).Members)
{
if (member.Metadata.Name == property.Name)
{
context.Context.Entry(member).State = EntityState.Detached;
}
}
}
}
I get the error: "The entity type 'ReferenceEntry' was not found. Ensure that the entity type has been added to the model." I use TEntity because I use more entities in a generic Class and use the same method to Update or Add. Thanks in advance.
Edit: If I use the entity (CurrentItem) like non generic type: (CurrentItem is now Customer, instead of TEntity)
context.Context.Entry(CurrentItem.Country).State = EntityState.Detached;
context.Context.SaveChanges();
Now works well. But I need to use TEntity.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)