EF Code First 学习笔记:关系
2016-05-16 00:43
381 查看
一对多关系
项目中最常用到的就是一对多关系了。Code First对一对多关系也有着很好的支持。很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性、导航属性等检测到模型之间的关系,自动为我们生成外键。观察下面的类:public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Destination { get; set; }
}
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Destination { get; set; }
}
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
}
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
//外键
public int TargetDestinationId { get; set; }
public Destination Target { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Target { get; set; }
//第一联系人
public Person PrimaryContact { get; set; }
//第二联系人
public Person SecondaryContact { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Lodging> PrimaryContactFor { get; set; }
public List<Lodging> SecondaryContactFor { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Target { get; set; }
//第一联系人
public Person PrimaryContact { get; set; }
//第二联系人
public Person SecondaryContact { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Lodging> PrimaryContactFor { get; set; }
public List<Lodging> SecondaryContactFor { get; set; }
}
public class Person
{
public int PersonId { get; set; }
public int SocialSecurityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public PersonPhoto Photo { get; set; }
}
public class PersonPhoto
{
[Key]
public int PersonId { get; set; }
public byte[] Photo { get; set; }
public string Caption { get; set; }
public Person PhotoOf { get; set; }
}
public class Person { public int PersonId { get; set; } public int SocialSecurityNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [Timestamp] public byte[] RowVersion { get; set; } public PersonPhoto Photo { get; set; } } public class PersonPhoto { [Key, ForeignKey("PhotoOf")] public int PersonId { get; set; } public byte[] Photo { get; set; } public string Caption { get; set; } public Person PhotoOf { get; set; } }
public class Person { public int PersonId { get; set; } public int SocialSecurityNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [Timestamp] public byte[] RowVersion { get; set; } public PersonPhoto Photo { get; set; } } public class PersonPhoto { [Key, ForeignKey("PhotoOf")] public int PersonId { get; set; } public byte[] Photo { get; set; } public string Caption { get; set; } public Person PhotoOf { get; set; } }
使用Fluent API:
modelBuilder.Entity<PersonPhoto>().HasRequired(p => p.PhotoOf).WithOptional(p => p.Photo);
注意:PersonPhoto表中的PersonId既是外键也必须是主键
public class User
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public bool IsActive { get; set; }
public System.DateTime CreateDate { get; set; }
public virtual Student Student { get; set; }
public virtual ICollection<Teacher> Teachers { get; set; }
public virtual UserDetail UserDetail { get; set; }
}
public class Teacher
{
[Key,ForeignKey("User")]
public int UserId { get; set; }
[Key, ForeignKey("Class")]
public int ClassId { get; set; }
[Key, ForeignKey("Course")]
public string CourseId { get; set; }
[Key, ForeignKey("Role")]
public int RoleId { get; set; }
public virtual Class Class { get; set; }
public virtual Course Course { get; set; }
public virtual Role Role { get; set; }
public virtual User User { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//一对一
modelBuilder.Entity<UserDetail>().HasRequired(p => p.User).WithOptional(p => p.UserDetail);
modelBuilder.Entity<Student>().HasRequired(p => p.User).WithOptional(p => p.Student);
//一对多
modelBuilder.Entity<Teacher>().HasKey(t => new { t.UserId, t.CourseId,t.ClassId,t.RoleId });
modelBuilder.Entity<Teacher>().HasRequired(p => p.User).WithMany(l => l.Teachers).HasForeignKey(p => p.UserId);
modelBuilder.Entity<Teacher>().HasRequired(p => p.Class).WithMany(l => l.Teachers).HasForeignKey(p => p.ClassId);
modelBuilder.Entity<Teacher>().HasRequired(p => p.Course).WithMany(l => l.Teachers).HasForeignKey(p => p.CourseId);
modelBuilder.Entity<Teacher>().HasRequired(p => p.Role).WithMany(l => l.Teachers).HasForeignKey(p => p.RoleId);
}
相关文章推荐
- 我在Adapter中使用holder时踩到的坑
- 数据结构之魔方程序
- 关于python字符串的处理
- python计算非内置数据类型占用内存
- android开发笔记之 AlarmManager(闹钟服务)
- KS-检验(Kolmogorov-Smirnov test) -- 检验数据是否符合某种分布
- Python 类(一)
- 再谈如何将android studio项目转换成eclipse
- C++11尝鲜:右值引用和转发型引用
- Win10应用商店、应用打不开或闪退的解决方法
- nginx学习笔记之三:nginx作为反向代理服务器
- Python内置函数(一)
- 2016"百度之星" - 资格赛(Astar Round1)Problem B(大数加法)
- Wininet笔记一
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 第一次机房收费颗粒归仓
- 2016年辛星less教程发布了
- iOS的查询、过滤(NSPredicate)
- Nginx详细配置说明
- 28. Implement strStr()