I have two entities referenced one to many. When entity framework created the table it creates two foreign keys, one for the key I have specified with the fluent interface and t
You must specify the many-end of the association explicitely:
modelBuilder.Entity()
.HasRequired(p => p.Department)
.WithMany(d => d.People)
.HasForeignKey(p => p.DepartmentId)
.WillCascadeOnDelete(false);
Otherwise EF will assume that there are two associations: One which is not exposed in Department
with the foreign key DepartmentId
and navigation property Department
in the Person
class as you have defined in the Fluent code - and another association which belongs to the exposed navigation property People
but with another not exposed end in Person
and a foreign key automatically created by EF. That's the other key you see in the database.