[EF6] How to select discriminator values from a single table?


I have two classes TypeA and TypeB inheriting from Account class. Type class has enum AccountType{TypeA,TypeB} and the rest of the properties are common in both classes. I use enum types in constructors to set the account type.

What I want to do is write a query to select all rows in Type table, which is differentiated by a discriminator column added by EF, and then add them to a list. I tried the following query which returns all types as a TypeA even though in the table there are rows from both types. When I try to add new Account with specified type, it is successfully written into the Accounts table with right discriminator.

//Using DbContext var query = db.Accounts.OrderBy(x => x.AccountNumber); foreach (var account in query) { allAccounts.Add(account); //Add to list } 

And then I tried fetching the types seperately, to no avail.

var query = db.Accounts.OfType<TypeA>(); var query2 = db.Accounts.OfType<TypeB>(); //Two foreach loops 

I've read that it is impossible to read the types from an EF table. Is it correct, or am I missing something? How can I set the type properties when creating a list from the table?

by pccopat via /r/csharp

Leave a Reply