将一个实体映射到多张数据库表
2012-04-21 22:30
267 查看
/article/5291744.html
将一个实体映射到多张数据库表,我们只能用Fluent API来做,Data Annotation无法满足要求,我们来看一下代码
View Code
这里需要注意的是,在PeopleConfig中,我们先对Person表做了映射,后对Detail表做了映射,则在生成的数据库中,Person表为主表,Detail表为从表,即Detail表的主键同时也为外键,生成的数据库如下:
将一个实体映射到多张数据库表,我们只能用Fluent API来做,Data Annotation无法满足要求,我们来看一下代码
View Code
View Code class People { public int Id { get; set; } public string Name { get; set; } public DateTime Birth { get; set; } public bool Sex { get; set; } public string Description { get; set; } } class myContext : DbContext { public DbSet<People> peopleSet { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new PeopleConfig()); } } class PeopleConfig : EntityTypeConfiguration<People> { public PeopleConfig() { Map(m => { m.Properties(p => new { p.Sex, p.Name }); m.ToTable("Person"); }); Map(m => { m.Properties(p => new {p.Description, p.Birth }); m.ToTable("Detail"); }); } }
这里需要注意的是,在PeopleConfig中,我们先对Person表做了映射,后对Detail表做了映射,则在生成的数据库中,Person表为主表,Detail表为从表,即Detail表的主键同时也为外键,生成的数据库如下:
相关文章推荐
- Code-First将一个实体映射到多张数据库表
- Hibernate处理一个实体映射多张相同结构的数据表--动态映射
- 将一个实体映射到多张表
- Hibernate处理一个实体映射多张相同结构的数据表--动态映射
- Hibernate处理一个实体映射多张相同结构的数据表--动态映射
- DBLayer wizard ,一个国外写的数据库访问和实体映射的自动生成工具
- hibernate 实体字段不映射到数据库
- 数据库的设计及hibernate实体映射
- Hibernate 实体类注解 Entity方式5、@Basic(fetch=FetchType,optional=true) 可选 @Basic表示一个简单的属性到数据库表的字段的映射,对于没有
- Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系
- 一步一步学EF系列四【升级篇 实体与数据库的映射】
- hibernate @Transeit 如何在映射实体中添加非数据库字段
- 【库房】EF映射——修改数据库表中的一个字段
- c#利用反射+特性实现简单的实体映射数据库操作类(表与类的映射)
- 数据库的设计及hibernate实体映射 【转】
- ssh如果对实体进行了修改,但不希望其修改映射到数据库中,修改不要放在service中
- Code-First将多个实体映射到一张数据库表
- 一个用泛型简单实现的数据库查询实体方法
- 跟着汤阳光同志做一个OA系统(六):系统管理、实体映射、类图设计
- Activiti 5.17 实体对象与类和数据库表的映射