Reputation: 13083
When we have two entities in EFv4 EDM diagram and only one table for both in the database (for instance, having table Documents and entities Invoice and Qoute), table Documents having documentTypeId column as a discriminator and set this column as a discriminator in the EDM (in Table mappings), how do we read the value of this property in our code?
We cannot assign values to it because EF does it for us under the hood (based on what we entered in Table mappings for condition) but somehow I don't get it why we are also not allowed to read it.
Upvotes: 2
Views: 509
Reputation: 364369
Imo this property is already mapped so you can't map it again. It is used to determine type of materialized entity. Why do you need such column. Usually it is enough to use is
operator like:
var document = context.Documents.GetById(id);
if (document is Invoice)
{
...
}
If you only need to select subtypes you can use OfType extension method like:
var invoices = context.Documents.OfType<Invoice>().ToList();
You also don't need to set this value when adding new entity because you are adding subtype - Invoice or Quote.
Edit:
As I understand from your comment you don't need this information in query. In such case you don't need to map it. Simply use partial class of your entity and add custom property which will return your string. Sound like stupid solution but actually it would be the easiest one.
Discriminator column should be part of mapping metadata so in case of T4 template generating your entities, it could be possible to update the template so it generate such property for you.
Upvotes: 2
Reputation: 156624
You may want to use a single-table inheritance hierarchy, as described here.
That way, you could have an abstract Document
class that includes a DocumentTypeId
column. Invoice
s and Quote
s would extend this class, but specify certain DocumentTypeId
filters. However, because the original class has a DocumentTypeId
column, they would each have that column as well.
Another advantage to this approach is that you could create utility methods that can act on any Document
, and you could pass any Invoice
or Quote
to these methods.
Upvotes: 0