Entity Framework (Database-First) multiple relations to same table naming conventions control
H

3

21

Let's suppose that we have this situation:

Tables in database:

Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person)

If we had to create the model from the database, using Entity Framework Database-First, in VS we'd have a class like this:

class Country {

int id;
string country_name;

virtual ICollection<Person> Person1; // Navigation Properties
virtual ICollection<Person> Person2; // ---------||----------

}

I've simplified the code a lot, but hopefully you got the point.

Seems that when Entity Framework deals with foreign keys it creates generic Navigation Properties. Is there a possibility to control how Navigation Properties are created by name? Person1, Person2 isn't very explainatory, unfortunately.

Honeymoon answered 5/11, 2014 at 13:0 Comment(3)
Personally, I prefer to use the Database-First methodology. And, I've had a lot of success using it. However, I enforce a strict naming convention within my databases. I have a utility script which will ripple through the database and rename my foreign-keys to match my naming convention. By the time the FKs get to the EDMX, their names actually make sense. Therefore, the EDMX makes sense, and the T4's get to generate code that makes sense.Proficiency
Could you share the script? I think that would be the closest option to solutionHoneymoon
@AdrianK. Did you find a solution for this?Heyduck
W
3

In VS you can do this with the GUI.

If you show Model Browser then navigate down the tree to:

YourEntityModel > Entity Types > Country

then right-click on the "Person1" Navigation Property and select "Properties" you can then change the name of the navigation property name to whatever you like:

enter image description here

Just change the name, save changes and your done...

(Actually there are lots of ways to get to the Navigation Property properties window - you cn right click on it in the model diagram too)

Williwaw answered 5/11, 2014 at 13:17 Comment(1)
I'm trying to avoid this method because if I regenerate the model from the database again it will overwrite the Name property you've shown hereHoneymoon
B
0

From the entry "Working with Inverse Navigation Properties" from the book "Programming Entity Framework: Code First":

You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.

You can place the annotations on either end of the relationship (or both ends if you want). We’ll stick them on the navigation properties in the Lodging class (Example 4-10). The InverseProperty Data Annotation needs the name of the corresponding navigation property in the related class as its parameter.

Example:

[InverseProperty("PrimaryContactFor")]
public Person PrimaryContact { get; set; }

[InverseProperty("SecondaryContactFor")]
public Person SecondaryContact { get; set; }
Bloomfield answered 5/11, 2014 at 13:21 Comment(3)
Is this update-proof? I mean, if I add a column to the table and regenerate the model, will this be overwritten?Honeymoon
I think that the OP is using Database First? He says "If we had to create the model from the database in VS..."Williwaw
I'm afraid not; that's one disadvantage from using the Database First-approach.Bloomfield
P
0

I recommend using https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838

It allows more flexibility than any database generation thing I've seen. I'm still working on solving my own problem, but this looks pretty promising. But, unlike the Default Code generation that EF provides, you can customize the mapping.

Like, in all the examples I've seen on renaming the navigation properties -- that alone won't be enough, because EF still needs to be mapped to use those navigation properties (you could hack it though, and have your User2 point to ModifiedByUser, for example).

Phila answered 8/2, 2016 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.