Different results from [ForeignKey] attribute vs. OnModelCreating or EntityTypeConfiguration

entity-framework entity-framework-5 entity-framework-6

Accepted Answer

It appears that your mapping is incorrect.

You have a navigation property becauseGroup It must be added in the mapping in the following manner:

modelBuilder.Entity<Person>()
    .HasOptional(t => t.Group)
    .WithMany(t => t.People) // <---
    .HasForeignKey(t => t.GroupId);

If not, EF will construct a new foreign key and utilize the navigation property to establish a different relationship between the two entities.

1
10/31/2013 8:44:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow