Entity Framework 'Update Model from Database' recreating associations between base and sub classes
Asked Answered
C

1

9

I am trying to implement a number of base/sub classes using Entity Framework, database first. Following some online tutorials, I have decided to attempt TPT inheritance.

On the database, I have a base class table 'Location', and two sub class tables: 'StreetAddress' and 'RuralRouteAddress'. I have defined a foreign key constraint between the sub class tables and the base class table on their primary keys. 'Location's Primary Key is an auto-increment column, and the two sub class tables' primary keys are not auto-increment.

In Entity Framework, I defined the 'Base Type' of the two sub classes as 'Location'. I then deleted the associations (and their corresponding navigation properties) from the model. I also deleted the ID column mappings from the sub classes, as ID is now inherited from the 'Location' base class.

This seems to have worked. I haven't tried updating/inserting, but querying returns the data with proper inheritance in place.

My problem is that, whenever I 'Update Model from Database', he inheritance association lines stay, but the FK associations between the base class and the sub classes are brought back... . I then have to delete them, and realign the association lines on my diagram (I'm a bit picky about the layout of the model diagram).

This isn't so bad, but the project that I would like to use TPT inheritance in has a lot of inheritance. Having to delete a ton of associations and reorganize my entire diagram every time I update the model is not very appealing.

Did I do something wrong when I implemented inheritance? Is there a way to ignore/exclude certain associations from being created when updating the model?

Cervin answered 23/1, 2018 at 18:37 Comment(10)
Did you also draw inheritance associations in the model? If I do that, the model survives a model update.Axum
@GertArnold The inheritance association lines were created for me when I set the Base Type on each child type. To clarify, the inheritance associations are not being removed when I update the model, but the navigation associations are being recreated, in addition to the inheritance. I'll edit my question to include that detail.Cervin
Did you "set the base type" in a partial class? If so, it looks like you have to add the inheritances in the edmx designer itself.Axum
@GertArnold No, I set the base type by selecting the sub entity table in the model diagram and then, in the properties panel, I set the 'Base Type' property to the base entity table. Everything was done in the edmx designer, nothing was done using partial classes.Cervin
OK, that's a way too. Well, when I do a similar thing in the edmx designer my model survives model updates. It's in VS2017, EF 6.2, DbContext. Any differences in your environment?Axum
I also can't reproduce - VS2017, EF6.1.3Cubital
@IvanStoev So, when you first created the edmx, did you have FK associations where you wanted your inheritance to be? Did you have to remove the FK associations and delete the primary keys on the subclasses manually? Once you did, the FK associations didn't come back for you after updating from database? I have seen this issue on two projects now. I am using VS 2015, but I've tried both EF6.1.3 and EF6.2Cervin
@Cervin Yes. I had to remove the FK associations as well as the Id property of the subclasses. Could it be VS issue then (the only difference I see so far is VS2017 vs VS2015)?Cubital
@IvanStoev It could be. I never would have thought it would be something specific about my version of VS. I'll test this on the weekend when I get a chance.Cervin
@Cervin Negative. Just checked with VS2015 and it doesn't happen.Cubital
N
0

The relationships you define in the database will always reappear when you update the model from the database. This is by design. If you want to have classes in the model that have a different relationship structure, try creating a complex model from a stored procedure that selects all the columns (or all the columns you want) from the base table. Import that procedure and in the Function Imports, edit the return type by creating a new complex type, or even just renaming the result that EF automatically creates. Then add your associations on that type, and use it as the base type for your inherited classes. The good part of this is that you can adjust the type structure to match any table changes by editing the stored procedure, then using "Get Column Information" and "Update" to bring the complex type into line. It won't overwrite your associations because they aren't defined in the database, but it is almost as straightforward as using TPT.

Joey

Nude answered 30/3, 2018 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.