I'm having a hard time getting some foreign keys to generate correctly in a migration.
This is what I'm trying to achieve (with some fields omitted for brevity):
![Image](http://s11.postimg.org/mrhh3tu0z/Capture.png)
My classes/Entities are as you'd expect:
```
public class PrivateExchange {
public int ID { get; set; }
public int AccountID { get; set; }
public int ExtensionLength { get; set; }
public string OutsideLinePrefix { get; set; }
// Other fields omitted...
public virtual ICollection<Extension> Extensions { get; set; }
public virtual ICollection<Branch> Branches { get; set; }
}
public class Branch {
public int ID { get; set; }
public int PrivateExchangeID { get; set; }
public int PurchasedQuantity { get; set; }
public string Features { get; set; }
public virtual PrivateExchange PrivateExchange { get; set; }
public virtual ICollection<Extension> Extensions { get; set; }
}
public abstract partial class Extension {
public int ID { get; set; }
public int PrivateExchangeID { get; set; }
public int BranchID { get; set; }
public string Number { get; set; }
public virtual PrivateExchange PrivateExchange { get; set; }
public virtual Branch Branch { get; set; }
}
public class Endpoint : Extension {
public EndpointStatuses Status { get; set; }
public string CLI { get; set; }
}
// Menu and queue are very similar to Endpoint, just with different fields. You get the idea.
// DbContext:
public class PBXContext : DbContext {
public IDbSet<PrivateExchange> PrivateExchanges { get; set; }
public IDbSet<Branch> Branches { get; set; }
public IDbSet<Extension> Extensions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.HasDefaultSchema("pbx");
modelBuilder.Entity<Endpoint>().ToTable("Endpoints");
modelBuilder.Entity<Menu>().ToTable("Menus");
modelBuilder.Entity<Queue>().ToTable("Queues");
base.OnModelCreating(modelBuilder);
}
}
```
When I generate the migration for this, the foreign keys on the Extensions table are all screwed up. It doesn't seem to recognise that PrivateExchangeID is the foreign key, and tries to create a new separate foreign key instead.
This is what I end up with:
```
CreateTable(
"vspbx.Branches",
c => new
{
ID = c.Int(nullable: false, identity: true),
PrivateExchangeID = c.Int(nullable: false),
PurchasedQuantity = c.Int(nullable: false),
Features = c.String(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("vspbx.PrivateExchanges", t => t.PrivateExchangeID, cascadeDelete: true)
.Index(t => t.PrivateExchangeID);
CreateTable(
"vspbx.Extensions",
c => new
{
ID = c.Int(nullable: false),
PrivateExchangeID = c.Int(nullable: false), // <- this is what I want the foreign key to be, but it ignores this.
BranchID = c.Int(nullable: false),
Number = c.String(),
PrivateExchange_ID = c.Int(), // <-- Where has this come from??
})
.PrimaryKey(t => t.ID)
.ForeignKey("vspbx.Branches", t => t.BranchID, cascadeDelete: true)
.ForeignKey("vspbx.PrivateExchanges", t => t.PrivateExchange_ID) // <-- This is wrong! should be PrivateExchangeID
.ForeignKey("vspbx.PrivateExchanges", t => t.ID) // <-- Eh??
.Index(t => t.ID)
.Index(t => t.BranchID)
.Index(t => t.PrivateExchange_ID);
CreateTable(
"vspbx.PrivateExchanges",
c => new
{
ID = c.Int(nullable: false, identity: true),
AccountID = c.Int(nullable: false),
ExtensionLength = c.Int(nullable: false),
OutsideLinePrefix = c.String(),
// ....
})
.PrimaryKey(t => t.ID);
CreateTable(
"vspbx.Endpoints",
c => new
{
ID = c.Int(nullable: false),
Status = c.Int(nullable: false),
CLI = c.String(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("vspbx.Extensions", t => t.ID)
.Index(t => t.ID);
```
I've marked the problems with comments.
Why is entity framework migrations adding in the extra PrivateExchange_ID foreign key when it should just use the property that I provided: PrivateExchangeID...?
Comments: Hey,
EF would introduce the `PrivateExchange_ID` column if it was not able to find a foreign key property by convention... though as you said, it should be detecting `PrivateExchangeID` as the foreign key property.
Based on the code you provided I'm not able to reproduce the issue. My guess is that there is something more complex in your full model that is causing EF to not detect the foreign key correctly.
Here is the code I used to try and reproduce it.
```
public class PrivateExchange
{
public int ID { get; set; }
public int AccountID { get; set; }
public int ExtensionLength { get; set; }
public string OutsideLinePrefix { get; set; }
public virtual ICollection<Extension> Extensions { get; set; }
public virtual ICollection<Branch> Branches { get; set; }
}
public class Branch
{
public int ID { get; set; }
public int PrivateExchangeID { get; set; }
public int PurchasedQuantity { get; set; }
public string Features { get; set; }
public virtual PrivateExchange PrivateExchange { get; set; }
public virtual ICollection<Extension> Extensions { get; set; }
}
public abstract partial class Extension
{
public int ID { get; set; }
public int PrivateExchangeID { get; set; }
public int BranchID { get; set; }
public string Number { get; set; }
public virtual PrivateExchange PrivateExchange { get; set; }
public virtual Branch Branch { get; set; }
}
public class Endpoint : Extension
{
public string CLI { get; set; }
}
public class PBXContext : DbContext
{
public IDbSet<PrivateExchange> PrivateExchanges { get; set; }
public IDbSet<Branch> Branches { get; set; }
public IDbSet<Extension> Extensions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("pbx");
modelBuilder.Entity<Endpoint>().ToTable("Endpoints");
}
}
```
Using EF6.1.3, it correctly detects the foreign key. Here is the code generated in the migration.
```
...
CreateTable(
"pbx.Extensions",
c => new
{
ID = c.Int(nullable: false, identity: true),
PrivateExchangeID = c.Int(nullable: false),
BranchID = c.Int(nullable: false),
Number = c.String(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("pbx.Branches", t => t.BranchID, cascadeDelete: true)
.ForeignKey("pbx.PrivateExchanges", t => t.PrivateExchangeID, cascadeDelete: true)
.Index(t => t.PrivateExchangeID)
.Index(t => t.BranchID);
...
```
Could you adjust the code listing above to something that reproduces the issue so that we can debug it for you.
~Rowan