Using DB-Default values for unmapped bit columns in entity framework 4 (database first) - entity-framework

I am trying to map two concrete entity types and an abstract base type to the same database table.
The table contains a bit column that does not accept null. Column has a default value: ((0)).
Only one of the two concrete entity types (i.e. concrete type 1) needs to use the column's value (for the other (i.e. concrete type 2) it is always false).
I tried adding a property mapped to that column only to the entity type which requires it
and a
When I call SaveChanges I get an UpdateException, with the following message on its inner-most-exception:
"The column cannot contain null values. [ Column name = MY_BIT_COLUMN,Table name = MY_TABLE ]"
I already edited the SSDL section of the EDMX and changed:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" />
to:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" DefaultValue="false" />
(Without this change mapping failed - will not run)
Is there any way to get around this without adding a property mapped to this column to the second concrete entity type or moving it to the base type?
Adding property as protected to concrete type 2 does work, but I would prefer a more elegant workaround.

If your workaround doesn't work (I'm little bit surprised but it is to late for me to test it now) then the only other workaround is changing your inheritance from TPH to TPT or TPC. The problem obviously is that TPH requires all columns in derived types to be nullable.
Other workaround is making your column member of parent entity and in derived entity use custom constructor which will always set it to false.
The last workaround is making your column nullable and enforce the validation in your business logic for the first entity.

Related

Discriminator Column Type

I've been looking at various Code First examples of TPT (Table Per Type) in Entity Framework.
I have an abstract base class with 4 concrete implementations, all of which share the exact same interface. These are being stored using EF in a single table named after the abstract base class.
What I wish to do is use the EF Discriminator column, but without using the automatic table creation in Code First, instead adding the configuration and mappings manually. Does anyone know if this would be possible and if so, what the type of the Discriminator column is (name, type, length, nullable, etc.) so I can create one manually?
Many thanks.

EntityFramework inheritance - Ignore not nullable column

I have an one entity in my edmx model having an one property that can contains huge XML data.
Basically I want to load this entity without this property (column) /* huge data loading */ . And load this column only when it is strictly needed.
I have tried to create an inherited entity containing this property and remove this property from base entity (original entity). I have done mapping.
At this time I have problem, that during compilation a I get error, that base entity is not capable to insert and update itself, because property is not nullable
I am looking for best approach (solution) how this situation should be solved.
I am attaching the cut-out from my emdx designer (containing my current and desired situation)
UPDATE:
I will try to write a procedure that I have tried:
I mapped functions to my custom functions. For entity TRP_TechReport_T without the XML column (property). Then I just mapped for entity TRP_TechReport_T functions to my custom function (containing XML column).
Then I set Mapping condition on the entity TRP_TechReport_T: When TRP_XML = Empty.String
TechReport_T mappings:
TechReport_T functions:
TechReportFull_T mappings:
TechReportFull_T functions:
At this moment I get error:
Error 2 Error 3032: Problem in mapping fragments starting at line 3754:Condition member 'TRP_TechReport_T.TRP_XML' with a condition other than 'IsNull=False' is mapped. Either remove the condition on TRP_TechReport_T.TRP_XML or remove it from the mapping.
The column is not nullable in the database and mustn't be.
I can hard-set XML property to nullable, but in the case of the model updating from the database information will be lost.
At the moment it's the only thing I could think of.

Entity Framework Table Per Hierarchy restrictions

I have very big table in my Database and a can't modify it.
So i have BaseEntity type for table.
I have several children (entity1, entity2) and i'd like to map each type to same column ("Date") and name properties differently.
Surely i can't move all same column properties to base type cause there is about 100 columns in my super table (it's not my design i've jst need to map it)
So i have 0019 error and is there any way to solve it or EF not for me?
No. TPH requires that each property defined in derived entity is exclusive for that entity (no other entity can map to the same column). This targets more general rule in EF - each column can be mapped only once. So if you need to use some column in more entities it must be defined in parent and must have same name in all child entities.

Entity Framework 4.1 Code First: How is the Discriminator determined?

Currently I have class hierarchy defined with the Code First approach as follows.
E.F. has autogenerated a nvarchar(128) discriminator. It is not a key field.
How does Entity Framework determine what and what Type the discriminator field should be, and is it always the same, i.e. nvarchar? Is the discriminator at all accessible outside the database i.e. from LINQ to Entity?
Discriminator column is by default nvarchar because it stores names of your classes to differ between types - that is the whole point of this column: to allow EF knowing what class instance from your inheritance hierarchy it should create when it loads record from the database.
Discriminator column is not accessible by linq-to-entities. It is only used to map record to correct type.

Not nullable fields in table inheritance - EDM

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.
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