Fluent Api配置实体之间的关系

一、一对一关系

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值