I cannot seem to get EF6 to generate a FK when I am using two-way navigation.
Here are my classes (removed some properties for brevity):
public class BaseSystem
{
public int Id { get; set; }
public SystemConfiguration Configuration { get; set; }
}
public class SystemConfiguration
{
public int Id { get; set; }
public BaseSystem System { get; set; }
}
I want the SystemConfiguration
table to have a FK reference back to BaseSystem.Id
.
Here's my fluent API code:
modelBuilder.Entity<SystemConfiguration>()
.HasRequired(x => x.System)
.WithOptional(x => x.Configuration);
When I use two-way navigation, this migration is generated which seems to generate the FK at first:
CreateTable(
"MYDB.SYSTEMCONFIGURATION",
c => new
{
ID = c.Decimal(nullable: false, precision: 10, scale: 0)
})
.PrimaryKey(t => t.ID)
.ForeignKey("MYDB.BASESYSTEM", t => t.ID);
After noticing that the SQL it generates doesn't include the FK, I removed the BaseSystem.Configuration
property and the migration generates this:
CreateTable(
"MYDB.SYSTEMCONFIGURATION",
c => new
{
ID = c.Decimal(nullable: false, precision: 10, scale: 0, identity: true),
SYSTEM_ID = c.Decimal(nullable: false, precision: 10, scale: 0),
})
.PrimaryKey(t => t.ID)
.ForeignKey("CISRF.BASESYSTEM", t => t.SYSTEM_ID, cascadeDelete: true);
My SQL also generates "SYSTEM_ID" number(10, 0) not null
as expected which it does not do when I have two-way nav.
What do I need to do to generate the FK on the SystemConfiguration
table while also still having two-way navigation?