-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
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- Could you provide a minimal project that shows this issue? |
@AndriySvyryd Of course, see attached. |
AndriySvyryd
added a commit
that referenced
this issue
Jul 21, 2016
Use ReferenceEqualityComparer in StateManager Fixes #6055
@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. |
👍 sounds good to me |
AndriySvyryd
added a commit
that referenced
this issue
Jul 21, 2016
Use ReferenceEqualityComparer in StateManager Fixes #6055
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
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
The text was updated successfully, but these errors were encountered: