網(wǎng)站首頁 編程語言 正文
概念
表拆分:一個表拆分成多個實體,例如Photograph表,可以拆分為Photograph和PhotographFullImage兩張表。
1、Photograph實體結(jié)構(gòu):
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實體結(jié)構(gòu):
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、創(chuàng)建數(shù)據(jù)上下文對象子類:
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 DbSetPhotographs { 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、使用數(shù)據(jù)遷移生成數(shù)據(jù)庫結(jié)構(gòu),查看生成后的結(jié)構(gòu):
5、寫入數(shù)據(jù)
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()) { // 寫入數(shù)據(jù) 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("創(chuàng)建成功"); Console.ReadKey(); } } }
?6、查詢數(shù)據(jù)
原文鏈接:https://www.cnblogs.com/dotnet261010/p/8007628.html
相關推薦
- 2022-12-06 詳解從ObjectPool到CAS指令_C#教程
- 2022-03-29 Python函數(shù)裝飾器的使用詳解_python
- 2022-10-03 Redis之SDS數(shù)據(jù)結(jié)構(gòu)的使用_Redis
- 2022-05-20 Spring-IOC—基于注解配置Bean
- 2022-03-24 Sublime?Text3安裝Go語言相關插件gosublime時搜不到gosublime的解決方法
- 2022-08-04 scrapy中的spider傳參實現(xiàn)增量的方法_python
- 2022-06-13 matplotlib繪制直方圖的基本配置(萬能模板案例)_python
- 2022-04-24 如何利用Python實現(xiàn)簡單C++程序范圍分析_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支