in Education by
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)

1 Answer

0 votes
by
I managed to solve the problem. Instead of inserting the property in the Entry, the value of the property had to be inserted. using (var context = new OpenContext(connectionString)) { var repository = context.UnitOfWork.GetRepository(); repository.Update(CurrentItem); // Get the type of TEntity Type typeOfTEntity = typeof(TEntity); foreach (var property in typeOfTEntity.GetProperties()) { // Check the properties that are virtual and not are HashSet if (property.GetGetMethod().IsVirtual && property.PropertyType.GenericTypeArguments.Count() == 0) { object value = property.GetValue(CurrentItem); // Check that value is not null and value type is an Entity if (value != null && value.GetType().IsSubclassOf(typeof(Entity))) { // Set the value that I don't want to change context.Context.Entry(value).State = EntityState.Detached; } } } context.UnitOfWork.SaveChanges(); } Thanks for the help.

Related questions

0 votes
    Im using EntityFramework Code First Generic Repository. I have one filter method. Also this method make paging ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have two entities: Job and Orderable on our code-first database: The (extremely simple) models of these ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I'm using EF code first. By convention, I have added a foreign key and a reference to the foreign ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    What is Dec Core 2020 Update?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    I am using EF for access MS SQL data with help LINQ. How can I get current isolation level ? For ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I am working with Entity Framework for a while but still have some "black holes" with how it is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following ... Any suggestions? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    On many sites/blogs I noticed that people, who want to explain linq mechanism, write Linq Queries and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    I have a Member class: public class Member { // key public Guid UserId { get; set; } // some ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    I writing a function in my form that require user to upload a file, but doesn't necessary have to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I've been using K8S ConfigMap and Secret to manage our properties. My design is pretty simple, that keeps ... 't been created Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How do you update the records with or without using Update Strategy?...
asked Mar 28, 2021 in Technology by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
...