簡體   English   中英

使用數據注釋指定外鍵

[英]Specify Foreign Key with Data Annotations

我正在使用最新版本的ASP.NET實體框架代碼優先。

如果我有兩個班級,如下所示:

public class Student
{
    public int ID { get; set; }
}

public class Classroom
{
    public int ID { get; set; }
    public Student Student { get; set; }
}

有沒有一種方法可以使用數據注釋,以便在生成Classroom表時知道我希望在Foreignroom中存在一個稱為StudentID的ForeignKey? 我知道如何用FluentAPI做到這一點,但是如果我能用數據注釋完成它,那將使我的應用程序更加簡單。 我還想避免在實際的對象模型中使用單獨的StudentID屬性,因為它似乎是重復的。

public class Student
{
public int ID { get; set; }
}

public class Classroom
{
public int ID { get; set; }
public Student Student { get; set; }

[ForeignKey("Student")]
public int StudentId {get; set;}



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM