user3655843
user3655843

Reputation: 33

Entity framework navigation properties naming convention

I have two tables named as Profile and ProfileHistory. Each record in ProfileHistory has to belong to a profile in Profile table, so there is a foreign key relation between two tables. Besides, in ProfileHistory table, there is a column named as ManagerId which also relates to Profile table with foreign key relation.

Profile table structure

Id int primary key .... ....

ProfileHistory table structure

Id int primary key

ProfileId int foreign key to Profile table

ManagerId int foreign key to Profile table

....

My question is: Since currently I only know this, I am creating my entity model from database. Model and therefore entity classes are created with navigation properties in ProfileHistory entity like following:

 public virtual Profile Profile { get; set; }
 public virtual Profile Profile1 { get; set; }

It is so confusing. Because it is not clear which navigation property for which relation. Even it is worse if I have more relations between two tables. navigation property names are becoming Profile, Profile1, Profile2, etc. I was expecting to have the name of the navigation properties related with its foreign key relations.

How can I make my navigation property names something that related to its foreign key relation, in my case "from Profile1 to ProfileManager" ?

Thank in advance for your kind helps.

Muharrem

Upvotes: 1

Views: 2381

Answers (4)

FirstDivision
FirstDivision

Reputation: 1430

The way I usually solve this is to add properties through partial classes that better represent what I'm after. This way if I need to delete the entity from the diagram and re-add it, I don't lose any renamed columns from the model.

The downside to this is that you need to remember that you cannot use them in Queries because EF won't know how to translate it into a SQL query. But if you've already got your Profile object, it's a lot easier to access myProfile.Manager than myProfile.Profile1.

So, for example, if EF created this for you:

public partial class ProfileHistory
{
    public virtual Profile Profile { get; set; }
    public virtual Profile Profile1 { get; set; }
}

I would end up creating a partial class like this to re-map the columns:

public partial class ProfileHistory
{
    public Profile Manager
    {
        get
        {
            return this.Profile1;
        }

        set
        {
            this.Profile1 = value;
        }
    }
}

Upvotes: 0

Complexity
Complexity

Reputation: 5830

I haven't tested it, but you can map a property to a column using an attribute:

[Column(“BlogDescription", TypeName="ntext")] 
public virtual Profile Profile { get; set; }

[Column("Profile1", TypeName="int")] 
public virtual Profile ProfileManager { get; set; }

Change the type and the name of the column as it is in the database.

Upvotes: 1

Andrew
Andrew

Reputation: 3806

I did face the same problem some time ago. Well, it is even bigger then just confusing names. If you have navigation properties to another table, like Profile, Profile1, Profile2, next you delete/edit the corresponding foreign keys you may end up having those mixed. And if you used EntitySQL to query data you'll end up having bugs because of incorrect data retrieved/wrong table join conditions...

What I did was changing the t4 template and modified the way properties are generated. When property code text is being written you have the information about association and foreign key related to it. Foreign key names are unique in database and I named those with following pattern

FK_[Table]_[Meaning]
...
FK_ProfileHistory_InitialProfile
FK_ProfileHistory_UpdatedProfile

Next, having this information, I named the properties with the [Meaning] part of the foreign key name.

Upvotes: -1

SoftwareFactor
SoftwareFactor

Reputation: 8588

You can always rename the properties in model diagram. The name can be found in Properties window when you click on a navigation property.

Upvotes: 1

Related Questions