In my application I’m using Entity Framework 4.0 Model First.
I have several tables that implement inheritance, like Product as a basetable and SpecificProduct that inherits from Product.
The inheritance is visible in the edmx file. To initially set up the database, I rightclick on the designer and select “Generate Database from Model”, which generates an SQL create script.
Let’s assume I do some changes in the SQL database to the SpecificProduct table and want to upgrade the model. Obviously I’d delete the table SpecificProduct from my edmx file and I’d rightclick on the edmx designer and select “Update Model from Database”.
This results in the inheritance between Product and SpecificProduct being lost. Instead of the inheritance I do have a 1 to 0..1 relationship and the primary key of Product is now also a column of SpecificProduct.
This is actually not the way I want it to be, because my project won’t build anymore, because my code relies on the inheritance being available.
I can manually fix this in the designer file by deleting the inserted primary key column into SpecificProduct, deleting the new 1 to 0..1 relationship, and inserting the inheritance in the edmx designer, again.
Ain’t there a way to do this automatically?
Or is this simply a limition of the Model First attempt, that I wasn’t aware of (and wouldn’t choose again, if this really is a limitation)?
You must not manually delete anything from EDMX file. Once you delete it your mapping is lost. The inheritance must be always mapped manually because database layer has no knowledge about it. You always start with basic relations which you must delete and change it to inheritance.
So in your case try to simply run update from database without deleting your entity. The new column should be added as a property to your entity. Btw. there is no swapping between model first and database first. Use the first approach or second approach. Combining them is not supported.