This is what I think is happening. When you only had the ParentIncident Code First, he matched this by convention as one end of a one-to-many self-regulatory, one-way, independent association. There is a lot of jargon - let me explain:
- Self-regulation: Like the apprarant from the question, the Indicent object is at both ends of the relationship.
- One-to-many: each pointer has one parent incident, and an incident can be the parent of many incidents.
- Unidirectional: there is a property of navigation from an Incident to its parent object (ParentIncident), but does not have any navigation properties for writing back child incidents.
- Indpendent Association: since there is no foreign key property in the class, EF creates an FK column in the database (called ParentIncident_IncidentId), but it does not map to any object.
Now, when you add the ClaimIncident property, this changes the way that Code First tries to match. Now he creates a self-tuning bi-directional FK association:
- Individually, because he sees two properties of navigation from incident to incident and suggests that these are two sides of the same relationship. Since none of them is a property of the collection, the relationship is one-to-one.
- Bidirectional again, because now there is a nvaigation property at each end of the relationship.
- FK, because the one-to-one relationship becomes PK in PK, and EF always considers this as the ratio of FK because FK (which is PK) is displayed.
But Code First cannot determine which side of this one-to-one relationship should be the main end and which should be the dependent end. And that sometimes matters ... so Code First throws.
Okay, so I'm going to infer from the stream and your model that you really don't want a one-to-one relationship here. (Individual binding of PK to PK for mutual binding is pointless, so you can argue that Code First should not create it, but Code First is not so smart.)
So you entered FK. Two things happen. First, Code First now suggests that it should be a one-to-many relationship. Second, Code First now has a name for the FK property and, in turn, uses this name to name the FK column in the database. But this name does not match the FK column already created for the independent associate - it is not ParentIncident_IncidentId. This means that Migrations will have to delete this column and create a new ... as a result of data loss.
To tell Code First that you really just want two independent, unidirectional, unidirectional, independent associations to use this smooth display:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Incident>() .HasOptional(e => e.ParentIncident) .WithMany(); modelBuilder.Entity<Incident>() .HasOptional(e => e.ClaimIncident) .WithMany(); }
Migrations should now properly update your database. Now I have to say that you can consider changing the FK associations, in which case you need to either make the data move in Migrations, just accept the data loss, if any, or tell Code First to continue using the FK column name that it previously created.