Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
DataAnnotations - ForeignKey Attribute:
ForeignKey attribute can be applied to properties of a class. Default Code-First convention for ForeignKey relationship expects foreign key property name match with primary key property.
Consider the following example.
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } //Foreign key for Standard
public int StandardId { get; set; } public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
As you can see in the above code, Student class includes foreign key StandardId, which is key property in Standard class. So now, Code First will create StandardId column in Students class as shown below.
ForeignKey attribute overrides this convention. You can have a different name of a foreign key property name than the primary key of a dependent class.
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } //Foreign key for Standard
public int StandardRefId { get; set; } [ForeignKey("StandardRefId")]
public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
As you can see in the above example, Student class includes StandardRefId foreign key property name instead of StandardId. We specify it using ForeignKey attribute on Standard navigation property so that Code-First will consider StandardRefId as foreign key, as shown below.
ForeignKey attribute can be applied to key property to specify which navigation property it points to, as shown below.
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } //Foreign key for Standard [ForeignKey("Standard")]
public int StandardRefId { get; set; } public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
Thus, you can use ForeignKey attribute to give a different name to a foreign key property.
Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute的更多相关文章
- Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute
DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...
- Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute
DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...
- Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute
DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...
- Entity Framework Code-First(9.8):DataAnnotations - Column Attribute
DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...
- Entity Framework Code-First(9.7):DataAnnotations - Table Attribute
DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...
- Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute
DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...
- Entity Framework Code-First(9.1):DataAnnotations - Key Attribute
DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...
- Entity Framework Code-First(9.4):DataAnnotations - Required Attribute
Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...
- Entity Framework Code-First(9.3):DataAnnotations - ConcurrencyCheck Attribute
ConcurrencyCheck Attribute: ConcurrencyCheck attribute can be applied to a property of a domain clas ...
随机推荐
- zookeeper学习与实战(一)环境部署
[背景]:最近需要做这样一件事,在一台服务器上通过客户端生成配置文件,然后需要将该配置文件实时的传输到上百台应用服务器,供应用程序读取.同时,该配置文件是不定时更新内容,只要有更新,远程应用服务器应该 ...
- nginx expires缓存提升网站负载
语法: expires [time|epoch|max|off]默认值: expires off作用域: http, server, location使用本指令可以控制HTTP应答中的“Expires ...
- 统计apachelog各访问状态个数(使用MapReduce)
统计日志文件中各访问状态的个数. 1.将日志数据上传到hdfs 路径 /mapreduce/data/apachelog/in 中 内容如下 ::::::: - - [/Feb/::: +] :::: ...
- win7系统查看端口占用情况
我们在启动应用或者在开发的时候的时候经常发现我们需要使用的端口被别的程序占用,但是我们又不知道是被谁占用,这时候我们需要找出“真凶”,如何做到呢? 方法/步骤 开始---->运行----&g ...
- hihocoder-1274 自行车架(高维dp)
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi的宿舍楼下有一块用于停自行车的区域.平时自行车都停得非常杂乱,于是楼长打算去买一排自行车架用来停车.自行车架一般有P个 ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- Agc017_D Game on Tree
传送门 题目大意 给定一棵树,$1$号节点为根,两个人轮流操作,每次选择一个根节点外的点,删掉它以及它的子树,不能操作者输,求两人均采用最优策略下先手胜利还是后手胜利. 题解 经典问题树上删边游戏,根 ...
- CH#56C 异象石 和 BZOJ3991 [SDOI2015]寻宝游戏
异象石 CH Round #56 - 国庆节欢乐赛 描述 Adera是Microsoft应用商店中的一款解谜游戏. 异象石是进入Adera中异时空的引导物,在Adera的异时空中有一张地图.这张地图上 ...
- python日志轮转RotatingFileHandler在django中的一个bug
简介 大量过时的日志会占用硬盘空间,甚至长时间运行不注意会占满硬盘导致宕机,那么就可以使用内建logging模块根据文件大小(logging.handlers.RotatingFileHandler) ...
- 景深(Depth of Field)
http://www.cnblogs.com/cxrs/archive/2013/03/22/DepthOfFeild.html 景深(Depth of Field) 什么是景深? 所谓景深,就是当焦 ...