简体   繁体   English

C# - EF 6抽象导航属性

[英]C# - EF 6 Abstract Navigation Property

I have the following abstract classes: 我有以下抽象类:

NotaFiscal: NotaFiscal:

public abstract partial class NotaFiscal
{   
    public virtual ICollection<NotaFiscalItem> NotaFiscalItens { get; set; }
}

NotaFiscalItem: NotaFiscalItem:

public abstract class NotaFiscalItem
{   
    ...
}

From which will be generated the concrete classes: 将从中生成具体类:

NotaFiscalEntrada: NotaFiscalEntrada:

public class NotaFiscalEntrada : NotaFiscal
{   
    public int NotaFiscalEntradaId { get; set; }
}

NotaFiscalEntradaItem: NotaFiscalEntradaItem:

public class NotaFiscalEntradaItem : NotaFiscalItem
{   
    public int NotaFiscalEntradaItemId { get; set; }
}

The question: The navigation property in the abstract class NotaFiscal is an collection of abstract objects, is there a way to navigate in the concrete class NotaFiscalEntrada to the objects in the collection, which will be concrete too - NotaFiscalEntradaItem ? 问题:抽象类NotaFiscal中的导航属性是抽象对象的集合,有没有办法在具体类NotaFiscalEntrada中导航到集合中的对象,这也是具体的 - NotaFiscalEntradaItem Is there a way to tell that in the concrete class NotaFiscalEntrada the ICollection of NotaFiscalItem will be the NotaFiscalEntradaItem and EF will understand this and navigate to it? 有没有办法告诉在具体类NotaFiscalEntrada中,NotaFiscalItem的ICollection将是NotaFiscalEntradaItem,EF会理解这一点并导航到它吗?

I gotta use it this way because the intelligence of the collection (LINQ queries, sum...etc..) is all in the abstract class, and others classes like NotaFiscalSaida and NotaFiscalItemSaida will be created from the abstract classes. 我必须这样使用它,因为集合的智能(LINQ查询,总和......等等)都在抽象类中,而其他类如NotaFiscalSaidaNotaFiscalItemSaida将从抽象类创建。 Each one will be a table in the DB. 每一个都是数据库中的一个表。

I'm using Code First, POCO, EF 6.1 and TPC mapping. 我正在使用Code First,POCO,EF 6.1和TPC映射。

Entity Framework does not support Generic Entities, but it does support Entities that are inheriting generic classes 实体框架不支持通用实体,但它确实支持继承泛型类的实体

Try to change your abstract NotaFiscal class to have a generic parameter to represent each NotaFiscalItem : 尝试将您的抽象NotaFiscal类更改为具有表示每个NotaFiscalItem的泛型参数:

public abstract class NotaFiscal<T> where T : NotaFiscalItem
{
    public abstract ICollection<T> NotaFiscalItems { get; set; }
}

Then in your concrete classe: 然后在你的具体classe:

public class NotaFiscalEntrada : NotaFiscal<NotaFiscalEntradaItem>
{
    public int NotaFiscalEntradaId { get; set; }

    public override ICollection<NotaFiscalEntradaItem> NotaFiscalItems { get; set; }
}

This way, your concrete NotaFiscal types will be able to expose their concrete NotaFiscalItem collection using the NotaFiscalItems property of each. 这样,您的具体NotaFiscal类型将能够使用每个类型的NotaFiscalItems属性公开其具体的NotaFiscalItem集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM