How to name foreign keys with database first approach

情到浓时终转凉″ 提交于 2019-12-01 03:32:01

问题


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.


回答1:


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

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;
        } 
    }
}


来源:https://stackoverflow.com/questions/24958482/how-to-name-foreign-keys-with-database-first-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!