Entity Framework 实体拆分

  把一个实体对象拆分到多个数据表中。例如:把一个博客Blog实体,拆分到多个表中。

(1)实体类

 1 public class Blog
 2 {
 3     public int Id { get; set; }
 4         
 5     public string Title { get; set; }
 6 
 7     public string Author { get; set; }
 8 
 9     public DateTime CreatedTime { get; set; }
10 
11     public string Context { get; set; }
12 }

(2)映射

 1 public class BlogMap : EntityTypeConfiguration<Blog>
 2 {
 3     public BlogMap()
 4     {
 5         this.HasKey(p => p.Id);
 6 
 7         this.Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
 8         this.Property(p => p.Title).IsRequired().HasMaxLength(63);
 9         this.Property(p => p.Author).IsRequired().HasMaxLength(31);
10         this.Property(p => p.CreatedTime).IsRequired();
11         this.Property(p => p.Context).IsOptional();
12 
13         this.Map(m =>
14         {
15               m.ToTable("Blog");
16               m.Properties(p => new { p.Id, p.Title, p.Author, p.CreatedTime });
17           }).Map(m => {
18               m.ToTable("BlogDetail");
19               m.Properties(p => new { p.Id, p.Context});
20         });
21     }
22 }

(3)实际效果

 

posted @ 2015-05-18 22:40  lcyan  阅读(98)  评论(0编辑  收藏  举报