views:

17

answers:

1

I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).

I was wondering, say I have a column 'HireDate' that need to use in the sub-class. That's for sure that in the DB it has to marked as nullable, but how can I mark it not nullable in the EDM? I tried to set it as not-nullable, but then it says that it needs a default value, and I want the default value to be DateTime.Now, not a constant value.

A: 

If a property (like HireData) is declared on a derived type and you are using TPH you should definitely be able to mark it as non-nullable in the EDM despite the fact it is nullable in the database.

In fact this ability is one of the characteristics of TPH.

...

So I looked at the blog post in question, and noticed it has a problem because it exposes the discriminator column (PersonCategory) as a property of the base Entity, which if allowed would make the type of an Entity mutable, since you could easily do this:

student.PersonCategory = 2;

Which would make Student an Administrator! And that is NOT allowed by the EF.

So if you are following this example closely that is likely to be your problem.

The column that holds the discriminator shouldn't be mapped to a Property in the EDM, it should only be used in the mapping (i.e. 'Add a Condition' under 'Maps to XXX' in the mapping window).

Solution to problem in Blog Post: Remove the PersonCategory property from Person class.

Anyway I hope this helps

Alex

Alex James