Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in TPT models with default values #23180

Closed
dagid4 opened this issue Nov 3, 2020 · 8 comments
Closed

Exception in TPT models with default values #23180

dagid4 opened this issue Nov 3, 2020 · 8 comments
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@dagid4
Copy link

dagid4 commented Nov 3, 2020

I use following version of EF Core:

Microsoft.EntityFrameworkCore.Sqlite 5.0.0-rc.2.20475.6

I have following classes:

public class Garage
{
    public int Id { get; set; }

    public List<Car> Cars { get; set; } = new List<Car>();
}

public class Car
{
    public int Id { get; set; }
}

public class Ferrari : Car
{
    public Special Special { get; set; }
}

public class Special
{
    public int Id { get; set; }
}

public class MyContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"Filename=my.db");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Garage>().ToTable("Garage");

        modelBuilder.Entity<Car>().ToTable("Car");

        modelBuilder.Entity<Ferrari>().ToTable("Ferrari");

        modelBuilder.Entity<Special>().ToTable("Special");
    }
}

I have following code:

await using var context = new MyContext();

await context.Database.EnsureCreatedAsync();

var garage = new Garage();

context.Add(garage);

await context.SaveChangesAsync();

var ferrari = new Ferrari();

garage.Cars.Add(ferrari);

ferrari.Special = new Special();

Console.WriteLine($"Special reference is null: {(ferrari.Special == null ? "YES" : "NO")}");

await context.SaveChangesAsync();

Console.WriteLine($"Special reference is null: {(ferrari.Special == null ? "YES" : "NO")}");

Reference to Special is getting nulled. The output is following:

Special is null: NO
Special is null: YES

You can try yourself: https://github.com/dagid4/EfCoreBug

In database everything is ok. Do you have please any idea how to solve this problem? Or is it just a bug?

It can be related to derived classes. If you move the Special property from Ferrari into Car, it is working.

@ajcvickers
Copy link
Member

I can reproduce this on the 5.0 daily build; it does not repro when switched to TPH. Could be an update pipeline or change tracking bug.
/cc @AndriySvyryd

@AndriySvyryd
Copy link
Member

@ajcvickers When propagating the store-generated value to the FK we set the FK store-generated value to null, due to #22603. When accepting the values we overwrite the propagated value with the "store-generated" null

@ajcvickers
Copy link
Member

@AndriySvyryd So, does TPT make this common enough that we should patch for #22603?

@AndriySvyryd
Copy link
Member

@ajcvickers Yes. In TPH there are no store-generated values in the Ferrari entry at that point so we don't have to reset them to null, however in TPT the Car part is inserted first and that triggers the creation of the store-generated Sidecar, leading to the situation above.

I haven't tried it, but inserting a dependent with 2 principals would likely hit this without TPT.

@AndriySvyryd AndriySvyryd added this to the 5.0.1 milestone Nov 4, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 4, 2020
@dagid4
Copy link
Author

dagid4 commented Nov 4, 2020

Thank you guys, this was driving me crazy. I've tried to build branch Issue23180 and it is now working! Hope it will get into daily builds soon.

AndriySvyryd added a commit that referenced this issue Nov 4, 2020
@kimimaru4000
Copy link

kimimaru4000 commented Nov 5, 2020

This looks like the issue I'm having on #23158, in which a dependent with two principals is being set to null, thus removing those now-null entries when saving changes again like @AndriySvyryd mentioned. Looking forward to the fix!

@tivtag
Copy link

tivtag commented Nov 10, 2020

@AndriySvyryd Will your fix be available in some nightly Alpha/Beta channel?
I believe that I am also hitting this problem.

@AndriySvyryd
Copy link
Member

@tivtag We don't publish nightly packages for patch releases to avoid overloading the NuGet source. You can try building the package yourself from the release/5.0 branch after #23186 is pushed or wait until December when we'll release 5.0.1

AndriySvyryd added a commit that referenced this issue Nov 12, 2020
@ajcvickers ajcvickers changed the title reference nulled after SaveChanges Exception in TPT models with default values Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

5 participants