简体   繁体   English

使用EF Core中的基类的导航属性

[英]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.

相关问题 EF Core - 索引中的导航属性 - EF Core - navigational property in index EF核心在保存时忽略导航属性 - EF core ignore navigational property on save 在 EF Core 中保存实体时,仅使用导航属性的 ID 会为该属性返回 null - When saving an entity in EF Core, using only an ID for a navigational property returns null for that property 我可以将具有导航属性的EF类用作控件的数据源吗? - Can I use EF class with navigational property as a datasource for my control? 无法使用OData,EF Core和AutoMapper映射List &lt;&gt;导航属性 - Unable to map List<> navigational properties using OData, EF Core and AutoMapper 如何在 EF Core 中使用基本 DbContext class 访问实体? - How to get to entities using base DbContext class in EF Core? EF Core 3.1 在添加新实体时重新插入现有导航属性 - EF Core 3.1 re-inserting existing navigational property when adding new entity 使用多对多导航属性更新EF中的实体 - Update entity in EF with many to many navigational property EF Core 3.0 可为空的导航属性 - EF Core 3.0 nullable navigational properties 如何使用EF6 Code First将外键属性公开给具有导航属性的现有实体 - How to expose Foreign Key property to existing entity having navigational property using EF6 Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM