Entity Framework表拆分
一、概念
表拆分:一个表拆分成多个实体,例如Photograph表,可以拆分为Photograph和PhotographFullImage两张表。
Photograph实体结构:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel.DataAnnotations; 4 using System.ComponentModel.DataAnnotations.Schema; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace CodeFirstTableSplit.Model 10 { 11 /// <summary> 12 /// 缩略图类 13 /// </summary> 14 public class Photograph 15 { 16 /// <summary> 17 /// 设置PhotoId是主键 自动增长 18 /// </summary> 19 [Key] 20 [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 21 public int PhotoId { get; set; } 22 23 public string Title { get; set; } 24 25 /// <summary> 26 /// 缩略图 27 /// </summary> 28 public byte[] ThumbnailBite { get; set; } 29 30 /// <summary> 31 /// Photograph通过导航属性引用PhotographFullImage 32 /// </summary> 33 [ForeignKey("PhotoId")] 34 public virtual PhotographFullImage PhotographFullImage { get; set; } 35 } 36 }
2、PhotographFullImage实体结构:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel.DataAnnotations; 4 using System.ComponentModel.DataAnnotations.Schema; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace CodeFirstTableSplit.Model 10 { 11 public class PhotographFullImage 12 { 13 [Key] 14 public int PhotoId { get; set; } 15 16 /// <summary> 17 /// 高分辨率 18 /// </summary> 19 public byte[] HighResolutionBits { get; set; } 20 21 /// <summary> 22 /// PhotographFullImage通过导航属性引用Photograph 23 /// </summary> 24 [ForeignKey("PhotoId")] 25 public virtual Photograph Photograph { get; set; } 26 } 27 }
3、创建数据上下文对象子类:
1 using CodeFirstTableSplit.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Data.Entity; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace CodeFirstTableSplit.DatabaseContext 10 { 11 public class EFDbContext :DbContext 12 { 13 public EFDbContext() 14 : base("name=Default") 15 { } 16 17 public DbSet<Photograph> Photographs { get; set; } 18 19 public DbSet<PhotographFullImage> PhotographFullImages { get; set; } 20 21 protected override void OnModelCreating(DbModelBuilder modelBuilder) 22 { 23 // 设置主体 24 modelBuilder.Entity<Photograph>().HasRequired(p => p.PhotographFullImage).WithRequiredPrincipal(t => t.Photograph); 25 26 // 生成同一张表:设置两个实体有相同的表名 27 modelBuilder.Entity<Photograph>().ToTable("Photograph"); 28 modelBuilder.Entity<PhotographFullImage>().ToTable("Photograph"); 29 base.OnModelCreating(modelBuilder); 30 } 31 32 33 } 34 }
4、使用数据迁移生成数据库结构,查看生成后的结构:
5、写入数据
1 using CodeFirstTableSplit.DatabaseContext; 2 using CodeFirstTableSplit.Model; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace CodeFirstTableSplit 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 using (var context = new EFDbContext()) 16 { 17 // 写入数据 18 byte[] thumbBits = new byte[100]; 19 byte[] fullBits = new byte[2000]; 20 var photo = new Photograph() { Title = "李四", ThumbnailBite = thumbBits }; 21 var fullImage = new PhotographFullImage() { HighResolutionBits = fullBits }; 22 23 photo.PhotographFullImage = fullImage; 24 context.Photographs.Add(photo); 25 // 保存 26 context.SaveChanges(); 27 } 28 Console.WriteLine("创建成功"); 29 Console.ReadKey(); 30 } 31 } 32 }
6、查询数据