我似乎无法在使用双向导航时使EF6生成FK。具有一对一关系和双向属性导航的EF6外键
这里是我的班(除去简洁一些属性):
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; }
}
我想SystemConfiguration
表有一个FK参考回到。
这里是我的一口流利的API代码:
modelBuilder.Entity<SystemConfiguration>()
.HasRequired(x => x.System)
.WithOptional(x => x.Configuration);
当我使用双向导航时,产生这种迁移,这似乎产生FK起初:
CreateTable(
"MYDB.SYSTEMCONFIGURATION",
c => new
{
ID = c.Decimal(nullable: false, precision: 10, scale: 0)
})
.PrimaryKey(t => t.ID)
.ForeignKey("MYDB.BASESYSTEM", t => t.ID);
注意到,在SQL后它生成的不包括FK,我删除了BaseSystem.Configuration
属性,迁移生成这个:
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);
我的SQL也会生成"SYSTEM_ID" number(10, 0) not null
,如果我有双向导航,它不会这样做。
我需要做什么来在SystemConfiguration
表上生成FK,同时还有双向导航?
您不能(EF限制) - 请参阅[EF Code First中的关联:第5部分 - 一对一外键关联](https://weblogs.asp.net/manavi/associations-in-ef -4-1 - 编码 - 第一部分-5-酮对一外键的协会)。 –