简体   繁体   English

实体框架导航属性命名约定

[英]Entity framework navigation properties naming convention

I have two tables named as Profile and ProfileHistory. 我有两个名为Profile和ProfileHistory的表。 Each record in ProfileHistory has to belong to a profile in Profile table, so there is a foreign key relation between two tables. ProfileHistory中的每个记录必须属于Profile表中的一个配置文件,因此两个表之间存在外键关系。 Besides, in ProfileHistory table, there is a column named as ManagerId which also relates to Profile table with foreign key relation. 此外,在ProfileHistory表中,有一列名为ManagerId,也与具有外键关系的Profile表相关。

Profile table structure 资料表结构

Id int primary key .... .... id int主键........

ProfileHistory table structure ProfileHistory表结构

Id int primary key id int主键

ProfileId int foreign key to Profile table ProfileId int Profile表的外键

ManagerId int foreign key to Profile table ManagerId int概要文件表的外键

.... ....

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: 使用ProfileHistory实体中的导航属性创建模型,并因此创建实体类,如下所示:

 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. 导航属性名称正在变为Profile,Profile1,Profile2等。我期望导航属性的名称与其外键关系相关。

How can I make my navigation property names something that related to its foreign key relation, in my case "from Profile1 to ProfileManager" ? 在我的情况下,如何使导航属性的名称与其外键关系相关,例如“从Profile1到ProfileManager”?

Thank in advance for your kind helps. 在此先感谢您的帮助。

Muharrem Muharrem

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. 更改数据库中列的类型和名称。

You can always rename the properties in model diagram. 您始终可以在模型图中重命名属性。 The name can be found in Properties window when you click on a navigation property. 单击导航属性时,可以在“属性”窗口中找到该名称。

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. 这样做的不利之处是您需要记住您不能在查询中使用它们,因为EF不知道如何将其转换为SQL查询。 But if you've already got your Profile object, it's a lot easier to access myProfile.Manager than myProfile.Profile1. 但是,如果您已经有了Profile对象,则访问myProfile.Manager比myProfile.Profile1容易得多。

So, for example, if EF created this for you: 因此,例如,如果EF为您创建了此文件:

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

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. 如果您具有到另一个表(如ProfileProfile1Profile2导航属性,那么接下来您将删除/编辑相应的外键,最终可能会混用这些外键。 And if you used EntitySQL to query data you'll end up having bugs because of incorrect data retrieved/wrong table join conditions... 而且,如果您使用EntitySQL查询数据,则由于错误的数据检索/错误的表连接条件而最终会出现错误……

What I did was changing the t4 template and modified the way properties are generated. 我所做的是更改t4模板并修改了属性生成的方式。 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. 接下来,有了这些信息,我使用外键名称的[Meaning]部分命名了属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM