内容简介:EF中关系映射也是一个很关键的内容,关系映射和属性映射一样,也是在我们今天先讲解在编写代码之前,我们先分析一下客户和订单的关系。一个客户可以有多个订单,但一个订单只能属于一个客户,所以我们用到了EF中的
EF中关系映射也是一个很关键的内容,关系映射和属性映射一样,也是在 OnModelCreating 中配置映射。EF中的关系映射有如下三种:
- One-to-Many Relationship(一对多)
- Many-to-Many Relationship(多对多)
- One-to-One Relationship(一对一)
我们今天先讲解 One-to-Many Relationship(一对一关系)
零、创建所需类
- 所有实体类公用的抽象基类
public abstract class Base
{
public int Id { get; set; }
public DateTime CreateTime { get; set; }
public DateTime ModifiedTime { get; set; }
}
- 客户类和订单类
public class Customer : Base
{
public string Name { get; set; }
public string Email { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public class Order : Base
{
public byte Quanatity { get; set; }
public int Price { get; set; }
public int CoustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
一、One-to-Many Relationship
- 创建Map映射类
在编写代码之前,我们先分析一下客户和订单的关系。一个客户可以有多个订单,但一个订单只能属于一个客户,所以我们用到了EF中的 HasRequired ,一个客户又存在多个订单,因此也使用到了 WithMany ,同时 Order 表中有 CustomerId 作为外键,因此我们用到了 HasForeignKey 。根据我们的分析,编写代码如下:
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
//数据库映射的表名称
ToTable("Customer");
//主键
HasKey(p => p.Id);
//属性映射的字段属性
Property(p => p.Name).HasColumnType("VARCHAR").HasMaxLength(50).IsRequired();
Property(p => p.Email).HasColumnType("VARCHAR").HasMaxLength(50).IsRequired();
Property(p => p.CreateTime);
Property(p => p.ModifiedTime);
//设置关系
HasMany(t => t.Orders).WithRequired(t => t.Customer).HasForeignKey(t => t.CoustomerId).WillCascadeOnDelete(false);
}
}
public class OrderMap : EntityTypeConfiguration<Order>
{
public OrderMap()
{
ToTable("Order");
HasKey(p => p.Id);
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Quanatity);
Property(p => p.Price);
Property(p => p.CoustomerId);
Property(p => p.CreateTime);
Property(p => p.ModifiedTime);
}
}
- 注册映射类
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typeToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => !String.IsNullOrEmpty(t.Namespace))
.Where(t => t.BaseType != null
&& t.BaseType.IsGenericType
&& t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach(var type in typeToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
中
注2:上述代码和描述是从客户的方向连编写的关系映射,如果以订单的角度来编写关系映射的话,只需删掉CustomerMap中的关系配置,在OrderMap中增加关系配置部分修改如下:
HasRequired(p => p.Customer).WithMany(p => p.Orders).HasForeignKey(p => p.CoustomerId).WillCascadeOnDelete(false);
运行控制台代码后,我们将在数据库看到表和表关系都被创建了:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 【mybatis xml】数据层框架应用--Mybatis(三)关系映射之一对一关系映射
- Hibernate 关系映射整理
- Hibernate 关系映射整理
- hibernate教程--关联关系的映射详解
- hibernate教程--关联关系的映射详解
- MyBatis学习笔记(2)—映射关系篇
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Development Recipes
Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00
You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!