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

SaveChanges: Wrong save order for one to many in combination with multi level inheritance #6055

Closed
Sebas- opened this issue Jul 12, 2016 · 4 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Sebas-
Copy link

Sebas- commented Jul 12, 2016

Steps to reproduce

Create 4 classes, Relation (abstract), Person, Employee and Address. Employee inherits from Person, and Person implements Relation.

public abstract class Relation {}
public class Person : Relation {}
public class Employee : Person {}
public class Address {}

Relation entity and Address entity have a primary key
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

Relation also has a collection property for Addresses
public virtual ICollection<Address> Addresses { get; set; }

plus some other properties.

The DbContext class has a DbSet for each entity except Address (does not matter for what i'm seeing).
public DbSet<Employee> Employees { get; set; }
public DbSet<Person> Persons { get; set; }
public DbSet<Relation> Relations { get; set; }

EF creates 2 tables in the database, as expected (TPH), using a discriminator for the Relations table.

Next, create a new instance of Employee, and add a new Address to the Adresses collection. Add the Employee to the Employees DbSet, and call SaveChanges on the context.

Do the same, but with Person and Persons DbSet.

The issue

When saving the Employee entity to the Employees DbSet, and the Employee Entity has 1 or more Addresses added it should first persist the Employee entity, and then the Address entities.

Currently it saves the Address entities first, resulting in Address entities without a relation to the Employee (Relation entity). Since there is no way to find these records they are 'lost' in the database.

This only happens when the entity is inheriting 1 class which implements another (as far as I can tell).

Note that the entry in the relation table is always correct. It has the discriminator that is expected (Person, Employee).

My expectation would be that it should fail instead of what is happening right now. (when a foreign key is added in the database, it does fail, as it should).

I have a more extensive test project that I can attach if needed.

Further technical details

EF Core version: 1.0.0
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL 1.0.0
Operating system: Windows 10
Visual Studio version: VS 2015 Update 3

@AndriySvyryd
Copy link
Member

@Sebas- Could you provide a minimal project that shows this issue?

@Sebas-
Copy link
Author

Sebas- commented Jul 14, 2016

@AndriySvyryd Of course, see attached.

EntityFrameworkTest1.zip

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 15, 2016
AndriySvyryd added a commit that referenced this issue Jul 21, 2016
Use ReferenceEqualityComparer in StateManager

Fixes #6055
@AndriySvyryd AndriySvyryd removed this from the 1.1.0 milestone Jul 21, 2016
@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jul 21, 2016

@ajcvickers @anpete @divega @rowanmiller I think this should be in 1.0.1. The bug can potentialy cause data loss and the fix is low risk.

@rowanmiller
Copy link
Contributor

👍 sounds good to me

@AndriySvyryd AndriySvyryd added this to the 1.0.1 milestone Jul 21, 2016
@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 Jul 21, 2016
AndriySvyryd added a commit that referenced this issue Jul 21, 2016
Use ReferenceEqualityComparer in StateManager

Fixes #6055
@AndriySvyryd AndriySvyryd removed their assignment Aug 18, 2016
@rowanmiller rowanmiller changed the title Wrong save order for one to many in combination with multi level inheritance SaveChanges: Wrong save order for one to many in combination with multi level inheritance Sep 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants