您现在的位置是:网站首页> 编程资料编程资料
Entity Framework表拆分为多个实体_实用技巧_
2023-05-24
502人已围观
简介 Entity Framework表拆分为多个实体_实用技巧_
概念
表拆分:一个表拆分成多个实体,例如Photograph表,可以拆分为Photograph和PhotographFullImage两张表。
1、Photograph实体结构:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirstTableSplit.Model { /// /// 缩略图类 /// public class Photograph { /// /// 设置PhotoId是主键 自动增长 /// [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int PhotoId { get; set; } public string Title { get; set; } /// /// 缩略图 /// public byte[] ThumbnailBite { get; set; } /// /// Photograph通过导航属性引用PhotographFullImage /// [ForeignKey("PhotoId")] public virtual PhotographFullImage PhotographFullImage { get; set; } } }2、PhotographFullImage实体结构:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirstTableSplit.Model { public class PhotographFullImage { [Key] public int PhotoId { get; set; } /// /// 高分辨率 /// public byte[] HighResolutionBits { get; set; } /// /// PhotographFullImage通过导航属性引用Photograph /// [ForeignKey("PhotoId")] public virtual Photograph Photograph { get; set; } } }3、创建数据上下文对象子类:
using CodeFirstTableSplit.Model; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirstTableSplit.DatabaseContext { public class EFDbContext :DbContext { public EFDbContext() : base("name=Default") { } public DbSet Photographs { get; set; } public DbSet PhotographFullImages { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 设置主体 modelBuilder.Entity().HasRequired(p => p.PhotographFullImage).WithRequiredPrincipal(t => t.Photograph); // 生成同一张表:设置两个实体有相同的表名 modelBuilder.Entity().ToTable("Photograph"); modelBuilder.Entity().ToTable("Photograph"); base.OnModelCreating(modelBuilder); } } } 4、使用数据迁移生成数据库结构,查看生成后的结构:

5、写入数据
using CodeFirstTableSplit.DatabaseContext; using CodeFirstTableSplit.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirstTableSplit { class Program { static void Main(string[] args) { using (var context = new EFDbContext()) { // 写入数据 byte[] thumbBits = new byte[100]; byte[] fullBits = new byte[2000]; var photo = new Photograph() { Title = "李四", ThumbnailBite = thumbBits }; var fullImage = new PhotographFullImage() { HighResolutionBits = fullBits }; photo.PhotographFullImage = fullImage; context.Photographs.Add(photo); // 保存 context.SaveChanges(); } Console.WriteLine("创建成功"); Console.ReadKey(); } } }6、查询数据

到此这篇关于Entity Framework表拆分为多个实体的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。
相关内容
- Entity Framework管理一对二实体关系_实用技巧_
- ASP.NET MVC自定义异常过滤器使用案例_实用技巧_
- ASP.NET MVC自定义异常过滤器_实用技巧_
- ASP.NET MVC授权过滤器用法_实用技巧_
- ASP.NET MVC实现区域路由_实用技巧_
- .NET 6中为record类型自定义Equals方法_实用技巧_
- 编译 dotnet和aspnetcore 源代码详情_实用技巧_
- ynamic LINQ创建高级查询服务_实用技巧_
- .NET中的HashSet及原理解析_实用技巧_
- .NET 6新特性试用之TryGetNonEnumeratedCount 方法_实用技巧_
