1

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

4 Answers4

1

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.

Flater
  • 12,908
  • 4
  • 39
  • 62
Complexity
  • 5,682
  • 6
  • 41
  • 84
1

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

SoftwareFactor
  • 8,430
  • 3
  • 30
  • 34
0

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;
        }
    }
}
FirstDivision
  • 1,340
  • 3
  • 17
  • 37
-1

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.

Andrew
  • 3,648
  • 1
  • 15
  • 29
  • -1 for no indication on how to do this - how is anyone supposed to use this answer if you haven't detailed where the changes are to be made?! – Bertie Apr 05 '17 at 14:04
  • do you want me to paste the t4 code here? You have the metadata context when you build up the information about each single property in the entity in that t4. Just use the info from the information on association – Andrew Apr 05 '17 at 15:30
  • Hi Andrew - it would be nice to give some snippets to indicate how to do this. I've managed to do a decent job of it now using some other resources though - but it might be nice for future readers. The post I found most helpful can be found here: http://stackoverflow.com/a/13064383/978562 – Bertie Apr 11 '17 at 09:46