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、查询数据

posted @   .NET开发菜鸟  阅读(505)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示