运维开发网

实体表被分割成多个实体

运维开发网 https://www.qedev.com 2022-09-13 18:13 出处:网络
这篇文章介绍了EntityFramework表拆分为多个实体的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 概念

这篇文章介绍了EntityFramework表拆分为多个实体的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 概念

分割表:一个表被分割成多个实体,比如photo表,它可以被分割成两个表:photo和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{ /// lt;summarygt; /// 缩略图类 /// lt;/summarygt; public class Photograph { /// lt;summarygt; /// 设置PhotoId是主键 自动增长 /// lt;/summarygt; [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int PhotoId { get; set; } public string Title { get; set; } /// lt;summarygt; /// 缩略图 /// lt;/summarygt; public byte[] ThumbnailBite { get; set; } /// lt;summarygt; /// Photograph通过导航属性引用PhotographFullImage /// lt;/summarygt; [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; } /// lt;summarygt; /// 高分辨率 /// lt;/summarygt; public byte[] HighResolutionBits { get; set; } /// lt;summarygt; /// PhotographFullImage通过导航属性引用Photograph /// lt;/summarygt; [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 DbSetlt;Photographgt; Photographs { get; set; } public DbSetlt;PhotographFullImagegt; PhotographFullImages { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 设置主体 modelBuilder.Entitylt;Photographgt;().HasRequired(p =gt; p.PhotographFullImage).WithRequiredPrincipal(t =gt; t.Photograph); // 生成同一张表:设置两个实体有相同的表名 modelBuilder.Entitylt;Photographgt;().ToTable("Photograph"); modelBuilder.Entitylt;PhotographFullImagegt;().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、查询数据


这就是本文关于将实体框架表分割成多个实体的内容。希望对大家的学习有所帮助

0

精彩评论

暂无评论...
验证码 换一张
取 消