EF Core 3.0 ...对于这种完全正常的映射,我找不到精确的答案。
主体到从属,没有反向指向主体的指针,关系为1:0,设置了类型对象/查找表。问题在于对象键名“ RunId”与EFCore生成的键名“ ServiceRunId”不同
如何使用Fluent API替换[ForeignKey(“ aServiceRun”))批注?
这是我当前的Fluent设置,但是我不知道将ForeignKey映射放在何处。
aBuilder.Entity <ServiceRun>()。HasKey(new string [] {“ RunId”});
aBuilder.Entity <Service>()。HasOne(s => s.aServiceRun);
Class Service {
public int ServiceId {get; set;}
[ForeignKey("aServiceRun")]
public int RunId { get; set; }
public virtual ServiceRun aServiceRun { get; set; }
}
Class ServiceRun {
public int RunId { get; set; }
public string description {get ;set; }
}
表格:
Service {
ServiceId int
RunId int
}
SerivceRun {
RunId int
Description string
}
如何使用Fluent API替换
[ForeignKey("aServiceRun")]
注释?
您正在寻找HasForeignKey流利的API。但是,为了获得对它(以及其他关系配置API)的访问权限,您需要使用,Has{One|Many}
然后使用来定义关系With{One|Many}
。对于一对一关系,您还需要提供以下通用类型参数HasForeignKey
:
使用Fluent API配置关系时,请使用
HasOne
和WithOne
方法。配置外键时,您需要指定从属实体类型-请注意
HasForeignKey
以下列表中提供的通用参数。在一对多关系中,很明显带有引用导航的实体是从属的,带有集合的实体是主体。但这不是一对一的关系-因此需要明确定义它。
请注意,包含FK的实体始终是从属的,因此在您的模型中,ServiceRun
是主体,Service
是从属的,流利的配置如下:
modelBuilder.Entity<Service>()
.HasOne(s => s.aServiceRun) // navigation property
.WithOne() // no navigation property
.HasForeignKey<Service>(s => s.RunId); // foreign key
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句