Edit
By carefully examining your example, I donβt think you are trying to do this. If you write a query by the type of the BASE object, you can only query fields that are defined in the base type.
Since "SomeProperty" is not defined in LogEntry, you cannot write this query:
var logEntries = db.LogEntry.Where(r => r.SomeProperty == "foo");
Since SomeProperty is not defined in the LogEntry class.
If you want to write queries to the base class, you need to do something like the following:
public class TPTContext : DbContext { public TPTContext() : base("name=TPT") { } public DbSet<BillingDetail> BillingDetails { get; set; } } public abstract class BillingDetail { public int BillingDetailId { get; set; } public string Owner { get; set; } public string Number { get; set; } } [Table("BankAccounts")] public class BankAccount : BillingDetail { public string BankName { get; set; } public string Swift { get; set; } } [Table("CreditCards")] public class CreditCard : BillingDetail { public int CardType { get; set; } public string ExpiryMonth { get; set; } public string ExpiryYear { get; set; } }
I wrote the following query for the base class:
TPTContext db = new TPTContext(); var allAccounts = db.BillingDetails.Where(b => b.Owner == "boran"); var bankAccounts = allAccounts.OfType<BankAccount>(); var creditCards = allAccounts.OfType<CreditCard>();
Everything seems to work well for me.
source share