[英]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 种方式声明外键:
- [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(NavigationPropertyName)] 依赖实体中的外键标量属性。
- [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] 在依赖实体中的相关参考导航属性上。
- [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,即在School
和Student
类中声明导航属性。 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.