一、一对一关系
1、两个实体分别包含一个引用属性,Code First默认约定它们为一对一关系。
2、在一对一关系情况下,需要提供给Code First额外的信息,以确定它们的主从关系。
配置一对一的方法:
HasRequired ,HasOptional ,WithOptional ,WithRequiredPrincipal,WithRequiredDependent
代码:
public class Person
{
public int PersonId { get; set; }
public int SocialSecurityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public byte[] RowVersion { get; set; }
public PersonPhoto Photo { get; set; }
}
public class PersonPhoto
{
public int PersonId { get; set; }
public byte[] Photo { get; set; }
public string Caption { get; set; }
public Person PhotoOf { get; set; }
}
1:0…1关系(主从关系)
因为Photo是具体人的,所以PersonPhoto使用PersonId作为主键。PersonPhoto必须属于一个Person,但是Person不一定有PersonPhoto,这种关系是1:0…1,此种情况下Person是一定存在的,所以它是主从关系主的一方。
出自PersonPhoto:
HasRequired(t => t.PhotoOf).WithOptional(t => t.Photo);
出自Person:
HasOptional(t => t.Photo).WithRequired(t => t.PhotoOf);
1:1
PersonPhoto必须属于一个Person,Person也必须有PersonPhoto,这种关系式1:1,此种情况下,两个都一定存在,要确定主从关系,需要使用WithRequiredPrincipal或WithRequiredDependent。
出自PersonPhoto:
HasRequired(t => t.PhotoOf).WithRequiredDependent(t => t.Photo);
出自Person:
HasRequired(t => t.Photo).WithRequiredPrincipal(t => t.PhotoOf)
二、一对多
1、两个实体,如果一个实体包含一个引用属性,另一个实体包含一个集合属性,Code First默认约定它们为一对多关系。
2、两个实体,如果只有一个实体包含一个导航属性或一个集合属性,Code First也默认约定它们是一对多关系。
配置一对多关系常用的方法有:
HasOptional ,HasRequired ,HasMany
Has方法后面往往跟着With方法
WithOptional ,WithRequired ,WithMany
代码:
1: public class Blog
2: {
3: public Blog()
4: {
5: Posts = new List<Post>();
6: }
7:
8: public int Id { get; set; }
9: public DateTime Creationdate { get; set; }
10: public string ShortDescription { get; set; }
11: public string Title { get; set; }
12: public List<Post> Posts { get; set; }
13: }
14:
15: public class Post
16: {
17: public int Id { get; set; }
18: public string Title { get; set; }
19: public string Content { get; set; }
20: public DateTime PostedDate { get; set; }
21:
22: public Nullable<int> BlogId { get; set; }
23: public virtual Blog Blog { get; set; }
24:
25: public int PrimaryAuthorId { get; set; }
26: public virtual Author PrimaryAuthor { get; set; }
27: public Nullable<int> SecondaryAuthorId { get; set; }
28: public virtual Author SecondaryAuthor { get; set; }
29: }
30:
31: public class Author
32: {
33: public int Id { get; set; }
34: public string Name { get; set; }
35: public string Email { get; set; }
36: //个人简历
37: public string Bio { get; set; }
38:
39: public List<Post> PrimaryAuthorFor { get; set; }
40: public List<Post> SecondaryAuthorFor { get; set; }
41: }
1:n
Post一定归属于一个Blog,这种关系是1:n。
出自Blog:
HasMany(x => x.Posts).WithRequired(x =>x.Blog)
出自Post:
HasRequired(x => x.Blog).WithMany(x => x.Posts)
0…1:n
Post可以单独存在,不用归属于Blog,这种关系是0…1:n。
出自Blog:
HasMany(x => x.Posts).WithOptional(x => x.Blog)
出自Post
HasOptional(x => x.Blog).WithMany(x => x.Posts)
设置外键
例如:
HasMany(x => x.Posts).WithOptional(x => x.Blog).HasForeignKey(x => x.BlogId)
设置级联删除
HasMany(x => x.Posts).WithOptional(x => x.Blog).HasForeignKey(x => x.BlogId).WillCascadeOnDelete();
推荐参考博客:
http://www.cnblogs.com/dudu/archive/2011/07/07/entity_framework_one_to_one.html