[英]Navigational property using base class in EF Core
I have a scenario where I have to use a base class for my entities where that base class refers to a property which is a foreign key to an entity I want to do the following: 我有一种情况,我必须为我的实体使用基类,其中该基类引用的属性是实体的外键,我想执行以下操作:
Billing Account --<- All Billable Entities 计费帐户-<-所有计费实体
public class BillingAccount:Entity
{
public int AccountId { get; set; }
public string AccountNumber { get; set; }
public int OwnerId { get; set; }
public decimal CurrentAccountBalance { get; set; }
public virtual Owner Owner { get; set; }
}
Here is the base Entity for all billable items 这是所有应付款项目的基础实体
public abstract class BillableEntity : Entity
{
public string BillingStatus { get; set; }
public string ReferencedInvoice { get; set; }
public bool RecordLocked { get; set; }
public int AccountId { get; set; }
public virtual BillingAccount Account { get; set; }
}
For my entities I am planning to extend or implement a base configuration for all billable entity 对于我的实体,我计划扩展或实施所有可计费实体的基本配置
public class BillableBaseConfiguration<T> : IEntityTypeConfiguration<T> where T : BillableEntity
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.HasOne(a => a.Account)
.WithMany()
.HasForeignKey(a => a.AccountId);
}
}
But I cannot figure it out how to implement from the base entity. 但是我无法弄清楚如何从基本实体中实现。 Is there a way or a workaround? 有没有办法或解决方法? Another way that i can think of is to create an SQL view, file up all the billable entity on that and then create a navigational property on the AccountId. 我能想到的另一种方法是创建一个SQL视图,将所有可计费实体归档,然后在AccountId上创建一个导航属性。
You have multiple options to define relationship one use Data Annotation and Add [ForeignKey("AccountId")]
if you use this approach then you do not need to use fluent api to define relations. 您可以使用多个选项来定义关系,一种方法是使用数据注释,然后添加[ForeignKey("AccountId")]
如果您使用此方法,则无需使用流畅的api来定义关系。 eg 例如
public int AccountId { get; set; }
[ForeignKey("AccountId")]
public virtual BillingAccount Account { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.