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

转载于:https://www.cnblogs.com/dgg2015/p/11152657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值