Last post Mar 20, 2015 02:23 PM by rover83
Mar 10, 2015 04:48 PM|rover83|LINK
I use Entity Framework version 6.1.2 with Visual Studio 2013, database first approach
I have created 3 inheritanced entities ( Table per Hierarchy (TPH) approach), DB has table named Offers,
I renamed to OfferBase class and used it as base abstract class :
I have set nullable options for all properties. For example, DeclinedDate mapped to AcceptedDeclinedDate field in table, which is allowed null. In my conceptual EF model DeclinedDate is not allowed null. I do it for other properties too. Ok, it compilable
After some time I want to update EF from database. I do it. After it all nullable options are broken:
So, all properties, which are nullable in DB layer (table) and mapped to inheritanced entities, clear to '(None)' after "update from database"
Mar 11, 2015 09:32 AM|itpreneur|LINK
When you apply database first, you must execute this only once and make all your POCO classes generated as your baseline model/entities and entityconfiguration that defines your relationship. Usually, we move this POCO generated classes into a separate projects
e.g. Model projects as part of your core projects.
At the same time, to establish and to be recognized it as part of your successive changes in the model, you must first run EF command lines such as enable-migrations, add-migrations, update database. These are very important steps in order to sync your code
first to database.
There's a lot of misconception about Database First Entity Framework and we thought that everytime we make changes on the database, everything will be sync automatically to our code and make use all POCO generated classes/methods. That's correct if there
are no changes in your code entities. But once you make changes such as implementing TPT/TPH, the story will be different. In this way, you have to go all the way Code first after your first Database first approach. Otherwise, all your changes will be discarded
since it will be overridden.
Best Practices approach
1. If you go all the way database first EF approach, make use of stored proc and generated complex type as your DTO/ViewModel classes.
2. If you go all the way Code First with existing database, Execute Database First approach only once to generate all POCO classes/methods and make it as your baseline entities/method and move it to a separate projects/etc. And make sure you initialize your
migration by following the paradigm...
a. enable-migrations <---execute once to create initial scripts and migrations folder and insert values in dbo.migration_history table.
b. add-migration <---execute this everytime you make changes in your model
c. update-database -script <---- execute this to create sql script that you can handover to dba or execute in your local....this will ensure that you have the right schema as you wanted....
Repeat b & c
Hope this helps. Happy EF Programming
Mar 12, 2015 05:29 AM|Sherwin Zhao|LINK
For you post, I think you need to read the following article. This step-by-step walkthrough shows how to implement table-per-hierarchy (TPH) inheritance in your conceptual model with the Entity Framework Designer (EF Designer).
And I think the import point as below:
Select Discriminator from the drop-down list.
In the Operator column of the Mapping Details window, select = from the drop-down list.
In the Value/Property column, type Instructor. The end result should look like this:
The reason we wanted to remove the Discriminator property, is because you cannot map a table column more than once. This column will be used for conditional mapping, so it cannot be used for property mapping as well. The only way it can be used for both, if a condition uses an Is Null or Is Not Null comparison.
Hope this can be helpful to you.
Mar 20, 2015 02:23 PM|rover83|LINK
I did the same way how it described in the article.
And it's not depend on Mapping problem and Discriminator.
To solve this problem:
Disabling syncing facets (right click on the designer surface and select properties, in the properties window find "Update Property Facets" and set it to "False") then try updating your model from DB