c# - Declare foreign keys using Data Annotation to use ReferentialConstraints collection?
问题描述
Referencing from this old post, I have tried out the voted answer using the sample code below. But it does not returns what the answerer have said it would. The ReferentialConstraints collection for the relationship between Student and Grade turns out to be null. Does anyone knows what I have done incorrectly?
Do I have to declare foreign keys using Data Annotation in order to have values in the ReferentialConstraints collection?
Code Sample for convention #1:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
}
解决方案
你可以使用这种类型
public int GradeId { get; set; }
[ForeignKey("GradeId")]
public Grade grade { get; set; }
推荐阅读
- flutter - GestureDetector 在堆栈小部件中不起作用
- android - 获取 ScrollView 的宽度并检测其末端
- linux - 通过 sql 查询传递列表中的元素的 shell 脚本
- typescript - 具有对象或字符串类型的打字稿数组无法访问对象键值
- r - 在没有外部计算的情况下在 ECDF 上添加点
- javascript - Firebase 函数未更新,函数返回未定义
- python-3.x - 如何在两台计算机之间建立连接以共享网络摄像头源?
- python - 当客户端连接时,服务器端流式传输 gRPC 应用程序显着减慢
- java - Java Android 从 Firestore 中获取集合的名称
- android - 我正在使用 youtube android player api 在我的应用程序中播放未列出的 youtube 视频,一次有多少用户可以访问它