Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

c# - 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?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...