Duplicate navigation properties when using Entity Framework 6 and fluent api

c# entity-framework entity-framework-6 fluent foreign-keys

Question

I have two models that have relationships.

public class Customer
{
    public Customer()
    {
        CustomerSites = new List<CustomerSite>();
    }     

    public IList<CustomerSite> CustomerSites { get; set; }
}

public class CustomerSite
{
    public Guid CustomerId { get; set; }
    public Customer Customer { get; set; }
}

Fluent api is used to build the relationships: (I have tried also with only one of the statement below)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<CustomerSite>()
            .HasRequired(x => x.Customer)
            .WithMany(x => x.CustomerSites)
            .HasForeignKey(x => x.CustomerId);

        modelBuilder.Entity<Customer>()
            .HasMany(x => x.CustomerSites)
            .WithRequired(x => x.Customer)
            .HasForeignKey(x => x.CustomerId);
}

Why does this code create double foreign keys for the database, as shown in the sample below, is my query.

 "dbo.CustomerSites",
            c => new
                {                     
                    CustomerId = c.Guid(nullable: false),                     
                    Customer_Id = c.Guid(),
                })
            .ForeignKey("dbo.Customers", t => t.CustomerId, cascadeDelete: true)
            .ForeignKey("dbo.Customers", t => t.Customer_Id)

Instead of Customer Id, there should only be one CustomerId. This is also nullable, as you can see. If I delete this column from the database context, an exception is thrown on update. I find this to be rather strange.

1
0
11/8/2013 4:47:05 PM

Popular Answer

ZZZ_tmp
0
1/11/2014 9:02:18 AM


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