Natan ZB Natan ZB - 1 month ago 15
C# Question

Trouble including properties in generic entities

Consider having QuotationForm, OrderForm, and DeliveryForm deriving from FormableEntity which is an abstract class.
In the QuotationForm, I have a collection (ICollection) of QuotationLine and other fields.

public class QuotationEntity : FormableEntity, ISummable
{
double DiscountPercent { get; set; }
public ICollection<QuotationLineEntity> Lines { get; set; } = new List<QuotationLineEntity>();
public double Total
{
get
{
double PercentOfTotal = 1.0 - (DiscountPercent / 100.0);
return Lines.Aggregate(0.0, (sum, line) => sum + line.Total) * PercentOfTotal;
}
}
}

public class QuotationLineEntity : ISummable, IEntityBase
{
int LineNumber { get; set; }
string Description { get; set; }
int Quantity { get; set; }
double UnitPrice { get; set; }
string DeliveryTime { get; set; }
string Notes { get; set; }
Guid ReferencingLine { get; set; }
string ReferencingMainAppId { get; set; }
public double Total
{
get
{
return UnitPrice * Quantity;
}
}
public Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public bool isActive { get; set; }
}


I have a DbContext, so far with this:

public DbSet<FormableEntity> Forms { get; set; }
public DbSet<QuotationLineEntity> QuotationLines { get; set; }


Now, in the Repository, I'm trying to get a form based on its Type
T
and Id:

IEnumerable<T> GetAll<T>(Guid id) where T : class, IEntityBase { WHAT SHOULD BE HERE? }


I was trying to check for the type and include the needed containers, like this:

if(typeof(T) == typeof(QuotationEntity)){
var set = m_context.Set<QuotationEntity>.Where(f => f.Id == id);
}


but then, the
.Include
does not work.
Also, I'm trying to be generic, so consider having OrderLines and DeliveryLines inside a OrderForm and DeliveryForm, and a base class for all the Line entities (including the QuotationLine), which means, that
public DbSet<QuotationLineEntity> QuotationLines { get; set; }
will change to
public DbSet<FormLineEntity> FormLines { get; set; }
. I find it impossible to achieve what is needed, any help?

Answer

You need to make your collections virtual. For further reading use this https://msdn.microsoft.com/en-us/data/jj574232(v=vs.113).aspx

Comments