Entity?Framework實(shí)體拆分多個(gè)表
一、概念
實(shí)體拆分:一個(gè)實(shí)體拆分成多個(gè)表,如Product實(shí)體,可以拆分成Product和ProductWebInfo兩個(gè)表,Product表用于存儲(chǔ)商品的字符類信息,ProductWebInfo用于存儲(chǔ)商品的圖片信息,兩張表通過SKU進(jìn)行關(guān)聯(lián)。
1、Product實(shí)體類結(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 實(shí)體拆分.Model { public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] //設(shè)置主鍵需要自己填充 public int SKU { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string ImageURL { get; set; } } }
2、數(shù)據(jù)實(shí)體類結(jié)構(gòu):
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; using 實(shí)體拆分.Model; namespace 實(shí)體拆分.DatabaseContext { public class EFDbContext :DbContext { public EFDbContext() : base("name=Default") { } public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>().Map(p => { p.Properties(m => new { m.SKU, m.Price, m.Description }); p.ToTable("Product"); }) .Map(p => { p.Properties(m => new { m.SKU, m.ImageURL }); p.ToTable("ProductWebInfo"); }); base.OnModelCreating(modelBuilder); } } }
3、使用數(shù)據(jù)遷移生成數(shù)據(jù)庫(kù),生成后的表結(jié)構(gòu)如下圖所示:
4、測(cè)試數(shù)據(jù):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using 實(shí)體拆分.DatabaseContext; namespace 實(shí)體拆分 { class Program { static void Main(string[] args) { using (var context = new EFDbContext()) { context.Products.Add(new Model.Product() { SKU=293, Description="C#高級(jí)編程(第10版)", Price=299 , ImageURL="http://image.baidu.com/1.jpg" }); // 保存 context.SaveChanges(); } Console.WriteLine("創(chuàng)建成功"); Console.ReadKey(); } } }
5、運(yùn)行程序,查詢數(shù)據(jù)庫(kù)結(jié)果
總結(jié)
將實(shí)體拆分成多表的步驟:
1、在工程中創(chuàng)建一個(gè)新類繼承自DbContext類。
2、創(chuàng)建Product的POCO類。
3、在新創(chuàng)建的DbContext子類中添加屬性:DbSet<Product>。
4、重寫DbContext類的OnModelCreating()方法。
到此這篇關(guān)于Entity Framework實(shí)體拆分多個(gè)表的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建一個(gè)ASP.NET MVC5項(xiàng)目的實(shí)現(xiàn)方法(圖文)
這篇文章主要介紹了創(chuàng)建一個(gè)ASP.NET MVC 5項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09不可或缺的ASP.NET內(nèi)置對(duì)象小結(jié)
這篇文章主要介紹了不可或缺的ASP.NET內(nèi)置對(duì)象小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
為大家講解下Repeater綁定dictionary數(shù)據(jù)源以及報(bào)錯(cuò)處理的方法,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04詳解ASP.NET Core 中基于工廠的中間件激活的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET Core 中基于工廠的中間件激活的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11.NET Core中創(chuàng)建和使用NuGet包的示例代碼
這篇文章主要介紹了.NET Core中創(chuàng)建和使用NuGet包的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04.Net Core中使用ref和Span<T>提高程序性能的實(shí)現(xiàn)代碼
這篇文章主要介紹了.Net Core中使用ref和Span<T>提高程序性能的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-05-051個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁(yè)面轉(zhuǎn)靜態(tài)頁(yè)面
這篇文章主要給大家介紹了關(guān)于如何通過1個(gè)文件輕松搞定Asp.net core 3.1動(dòng)態(tài)頁(yè)面轉(zhuǎn)靜態(tài)頁(yè)面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.net core 3.1具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
這篇文章主要給大家介紹了關(guān)于如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02