简体   繁体   English

如何使用 EF 6 在另一个表中为外键添加列描述?

[英]How do you add column description for a foreign key in another table utilizing EF 6?

Referring to this previous post: How to add description to columns in Entity Framework 4.3 code first using migrations?参考上一篇文章: 如何首先使用迁移向实体框架 4.3 代码中的列添加描述?

I have successfully implemented the modified solution proposed by user Abdullah but I have encountered the exception below:我已经成功实施了用户Abdullah提出的修改后的解决方案,但我遇到了以下异常:

System.Data.SqlClient.SqlException HResult=0x80131904 Message=Object is invalid. System.Data.SqlClient.SqlException HResult=0x80131904 消息=对象无效。 Extended properties are not permitted on 'dbo.School.Students', or the object does not exist. “dbo.School.Students”上不允许使用扩展属性,或者对象不存在。

Sample code as below:示例代码如下:

public class School
{
    public School()
    {
        Students = new HashSet<Student>();
    }
    
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public Guid Id { get; set; }

    [Description("Some Text")]
    public string Description { get; set; }
    
    [Description("Some text")]
    public ICollection<Student> Students{ get; set; }
}

I understand from the exception message, there is no column generated for Students.我从异常消息中了解到,没有为学生生成列。 Checking the DB shows that the Student table have the column SchoolId as FK.检查数据库显示学生表的列 SchoolId 为 FK。

So the question here is: how do I go about adding/updating the FK description when EF generates the FK column in another table?所以这里的问题是:当 EF 在另一个表中生成 FK 列时,我该如何添加/更新 FK 描述?

As you already understand the foreign key is defined in Student entity and the field is SchoolId so you have to add descriptor there.正如您已经了解的那样,外键是在 Student 实体中定义的,并且该字段是 SchoolId,因此您必须在那里添加描述符。 The Students property in the School entity is the navigation property; School 实体中的 Students 属性是导航属性; a feature from EF to easily get the list of all the students of a particular school. EF 的一项功能,可轻松获取特定学校的所有学生列表。

Although he did not answer my question, I have to thank Raihan for giving me the idea to check again on how Foreign Keys can be declared.虽然他没有回答我的问题,但我还是要感谢Raihan给了我再次检查如何声明外键的想法。 :) :)

Based on Entity Framework Tutorial , a foreign key can be declared 3 ways:基于Entity Framework Tutorial ,可以通过 3 种方式声明外键:

  1. [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(NavigationPropertyName)] 依赖实体中的外键标量属性。
  2. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] 在依赖实体中的相关参考导航属性上。
  3. [ForeignKey(ForeignKeyPropertyName)] on the navigation property in the principal entity. [ForeignKey(ForeignKeyPropertyName)] 在主体实体中的导航属性上。

Also referencing to how a one-many relationship can be declared section in Entity Framework Tutorial website, we can see that the method which I had described in my question is Convention 2.还参考如何在实体框架教程网站中声明一对多关系部分,我们可以看到我在问题中描述的方法是约定 2。

In order for User Raihan's suggestion to work, I will need to change the one-many declaration to Convention 3, which is to declare a navigational properties at both the School and Student classes.为了让用户 Raihan 的建议起作用,我需要将一对多声明更改为约定 3,即在SchoolStudent类中声明导航属性。 Like below:像下面这样:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public School School { get; set; }
}

public class School
{
    public int GradeID { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }

    public ICollection<Student> Student { get; set; }
}

Referencing the foreign key declaration sample in the same website, the Student class needs to be further modified as shown below in order for the (Description) attribute to work:引用同一网站中的外键声明示例,需要进一步修改Student类,如下所示,以使 (Description) 属性起作用:

public class Student
{
    [Description("Id of Student")]
    [Key]
    public int Id { get; set; }
    [Description("Name of Student")]
    public string Name { get; set; }

    [Description("Some Description")]
    public int SchoolId {get; set;}
    public School School { get; set; }
}

I am still looking for a shorter way to get the column description in so please do reply if you think your solution is more elegant than mine.. :)我仍在寻找一种更短的方法来获取列描述,所以如果您认为您的解决方案比我的更优雅,请回复。:)

暂无
暂无

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

相关问题 如何在ADO.Net实体模型中将具有外键的表更新到另一个表? - How do you update a table with a foreign key to another table in ADO.Net Entity Model? EF代码优先-如何为不同类型的父母使用的子对象表指定外键名称 - EF Code First - How do you specify foreign key name for child object table used by different type of parents 如何通过使用原始表中的“ Customer Name”(客户名称)之类的列来将外键插入联结表中以引用该键 - How do you insert a foreign key into a junction table by using a column like “Customer Name” in the original table to reference the key 如何使用ASP.NET MVC中该表的另一列获取外键表中的记录ID? - How do I get the id of record in foreign key table using another column in that table in ASP.NET MVC? 如何在没有包含在EF模型中的外键的情况下从一个表到多个表列中获取值 - How to get values from a one to many table column without foreign key included in EF model 我想在另一个表中添加 AspNetUsers 表“Id”列作为外键 - I want to add AspNetUsers table "Id" column as Foreign Key in another table EF-如何定义外键,即当前表的主键 - EF - How to define foreign key which is the primary key of the current table 如何将一个表中的主(代理)键添加到另一个表的外键中? - How to add a primary (Surrogate) key from one table into a foreign key of another table? 如何向 EF 核心中的现有数据添加新的外键属性? - How to add a new foreign key property to existing data in EF core? 如何在 EF Core 中的不同项目之间向 ApplicationUser 添加外键? - How to add a foreign key to ApplicationUser between separate project in EF Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM