C # - EF 6 Abstract Navigational Property

I have the following abstract classes:

NotaFiscal:

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

      

NotaFiscalItem:

public abstract class NotaFiscalItem
{   
    ...
}

      

From which concrete classes will be generated:

NotaFiscalEntrada:

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

      

NotaFiscalEntradaItem:

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

      

Question: The navigation property in the abstract class NotaFiscal is a collection of abstract objects, is there a way to navigate in the concrete class NotaFiscalEntrada to objects in the collection that will also be concrete - NotaFiscalEntradaItem? Is there a way to say that in a specific NotaFiscalEntrada class ICollection NotaFiscalItem will be NotaFiscalEntradaItem and EF will figure it out and jump to it?

I have to use it this way because collection intelligence (LINQ queries, sum ... etc) is all in an abstract class, and other classes like NotaFiscalSaida and NotaFiscalItemSaida will be generated from abstract classes. Each of them will be a table in the database.

I am using Code First, POCO, EF 6.1 and TPC mapping.

+3


source to share


1 answer


Entity Framework does not support Generic Entities, but does support Entities inheriting generic classes

Try changing your abstract class NotaFiscal

to have a common parameter to represent each NotaFiscalItem

:

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

      



Then in your concrete class:

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

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

      

This way, your specific types NotaFiscal

will be able to expose their specific collection NotaFiscalItem

using a property NotaFiscalItems

on each of them.

+3


source







All Articles