EF Core 2.1 [change tracking] - saving all related entities

thatsallfolks

Maybe the title is misunderstood but I'm gonna try to explain my situation here.

So, considering my entity Person that has a virtual property Country, as follow:

public class Person {
    public long Id { get; set;}
    public short IdCountry { get; set;}
    public virtual Country Country { get; set; }
    ...
}

and the mapping as follow:

...
builder.HasOne(c => c.Country)
    .WithMany()
    .HasForeignKey(c => c.IdCountry);

and my repository is generic.

What happens here is, when I try to save some change in Person, the Country entity state came to me as Added wich does not make sense.

I'm exposing here a similar example that what is happening to me. Actually, I have like five others virtual properties in my class Person and some of them are in this state (Added).

Has anyone ever dealt with it before? Thanks in advance.


EDIT: Ansewing @alans reply: I'm doing something similar as follow:

foreach (var entrie in entry.Context.ChangeTracker.Entries())
{
    try
    {
        key = entrie.Entity.GetType().GetProperty("Id").GetValue(entrie.Entity, null);
    }
    catch (Exception ex)
    {
        key = null;
    }
    if (key != null && entrie.State == EntityState.Added)
    {
        entrie.State = EntityState.Unchanged;
    }
    else if (Convert.ChangeType(key, typeof(long)) as long? == 0)
    {
        entrie.State = EntityState.Added;
    }
}
_dbContext.SaveChanges();

@TanvirArjel this method above is where the state come as Added

thatsallfolks

I was using AsNoTracking() extension method for performance questions in EF6 but I realized that in EFCore this particular method is a little bit different (I was migrating the code). So, I only removed the method

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Entity Framework Core 2.0.1 Eager Loading on all nested related entities

EF Core: loading related entities - circular dependency

Querying related entities with Eager & Explicit loading it doesn't work in EF for ASP.NET MVC CORE

Tracking table changes in ASP.NET core 2 and EF

EF Core Select one Entity filtered by related entities

How to add the same column to all entities in EF Core?

Eager loading related entities in EF Core using EF6 syntax

how to apply common configuration to all entities in ef core

EF Core 2.0 does not load related entities when using where condition

Filtering "include" entities in EF Core

How to enable change tracking for ALL entities in Dynamics 365?

EF Core change tracking - issue with original values and altered values

EF Core - Not loading all child entities

How to load entity in EF Core with the 'AsNoTracking' method in combination with explicitly loading of related entities

Generic method for setting string enum converter in EF Core for all entities

EF core 3.1: should I initialize list navigation properties when using eager loading to load related entities?

Delete all related rows to user using EF .NET CORE

EF Core: Correct way to query data multiple levels deep in related one-to-many entities

Running a query over all entities in EF Core

Making a temporary ID for entities in EF before saving

Sort related entities in core data

How do I get my DataBase First EF 6.1 Entities to become a change tracking proxies

How to load all related EF entities in preparation for serialization

One To many : 1 Entity has 2 entities related

EF Core not saving ParentId

EF Core use stored procedure to load related entities

Select with JOIN, Group BY and SUM in related 1:n tables (2nd level of child table) with LINQ / EF-Core

Select with JOIN, Group BY and SUM in related 1:n tables (2nd level of child table) with LINQ / EF-Core

How avoid that EF Core 3.1 load related entities on where clause