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

本小妞迷上赌 提交于 2020-11-25 04:32:30

问题


Referring to this previous post: How to add description to columns in Entity Framework 4.3 code first using migrations?

I have successfully implemented the modified solution proposed by user Abdullah but I have encountered the exception below:

System.Data.SqlClient.SqlException HResult=0x80131904 Message=Object is invalid. Extended properties are not permitted on 'dbo.School.Students', or the object does not exist.

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.

So the question here is: how do I go about adding/updating the FK description when EF generates the FK column in another table?


回答1:


As you already understand the foreign key is defined in Student entity and the field is SchoolId so you have to add descriptor there. The Students property in the School entity is the navigation property; a feature from EF to easily get the list of all the students of a particular school.




回答2:


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. :)

Based on Entity Framework Tutorial, a foreign key can be declared 3 ways:

  1. [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity.
  2. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.
  3. [ForeignKey(ForeignKeyPropertyName)] on the navigation property in the principal entity.

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.

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. 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:

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.. :)



来源:https://stackoverflow.com/questions/64656608/how-do-you-add-column-description-for-a-foreign-key-in-another-table-utilizing-e

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