網站首頁 編程語言 正文
概念
表拆分:一個表拆分成多個實體,例如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 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、使用數據遷移生成數據庫結構,查看生成后的結構:
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、查詢數據
原文鏈接:https://www.cnblogs.com/dotnet261010/p/8007628.html
相關推薦
- 2023-01-10 Flutter?SystemChrome使用方法詳解_Android
- 2022-05-23 Docker容器鏡像相關命令基本介紹與使用_docker
- 2022-06-01 Python讀取csv文件做K-means分析詳情_python
- 2022-07-02 Python使用?TCP協議實現智能聊天機器人功能_python
- 2022-08-03 C#?代碼大小寫規范說明_C#教程
- 2022-12-09 python中為main方法傳參問題_python
- 2022-11-10 利用C++實現獲取文件夾下所有文件名_C 語言
- 2022-07-22 Math.ceil() 函數使用介紹
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支