Marc
Marc

Reputation: 16512

How to name foreign keys with database first approach

Is there a way to change the naming convention with Entity Framework?

Example :

I have 2 tables

Planification
--------------
creator_id <fk>
guest_id <fk>


Profile
--------------
profile_id <pk>

creator_id and guest_id are both foreign keys for the profile_id

by default, entity would generate a planification class like this

public string guest_id { get; set; }
public string creator_id { get; set; }

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

I would prefer something more specific than Profile and Profile1 like Guest and Creator.

Is there a way to change the naming convention because I feel really guilty letting it like this.

Upvotes: 6

Views: 2545

Answers (1)

jimSampica
jimSampica

Reputation: 12410

In your edmx you can rename the nav properties by clicking on the property and changing the Name.

enter image description here

Note that if you delete the table and build it from the database again you will have to rename it in the edmx.

If you are sufficiently annoyed by this. A way to get around it is to instead use a partial class with a property that gives a name to the default-named property.

public partial class Planification
{
    public Profile Creator 
    { 
        get{ return this.Profile1; }
        set{
            this.Profile1 = value;
        } 
    }

    public Profile Guest 
    { 
        get{ return this.Profile; }
        set{
            this.Profile = value;
        } 
    }
}

Upvotes: 8

Related Questions