Entity?Framework實體拆分多個表
一、概念
實體拆分:一個實體拆分成多個表,如Product實體,可以拆分成Product和ProductWebInfo兩個表,Product表用于存儲商品的字符類信息,ProductWebInfo用于存儲商品的圖片信息,兩張表通過SKU進(jìn)行關(guān)聯(lián)。
1、Product實體類結(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 實體拆分.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ù)實體類結(jié)構(gòu):
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 實體拆分.Model;
namespace 實體拆分.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ù)庫,生成后的表結(jié)構(gòu)如下圖所示:

4、測試數(shù)據(jù):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 實體拆分.DatabaseContext;
namespace 實體拆分
{
class Program
{
static void Main(string[] args)
{
using (var context = new EFDbContext())
{
context.Products.Add(new Model.Product() {
SKU=293,
Description="C#高級編程(第10版)",
Price=299 ,
ImageURL="http://image.baidu.com/1.jpg"
});
// 保存
context.SaveChanges();
}
Console.WriteLine("創(chuàng)建成功");
Console.ReadKey();
}
}
}5、運行程序,查詢數(shù)據(jù)庫結(jié)果

總結(jié)
將實體拆分成多表的步驟:
1、在工程中創(chuàng)建一個新類繼承自DbContext類。
2、創(chuàng)建Product的POCO類。
3、在新創(chuàng)建的DbContext子類中添加屬性:DbSet<Product>。
4、重寫DbContext類的OnModelCreating()方法。
到此這篇關(guān)于Entity Framework實體拆分多個表的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
web.config中配置數(shù)據(jù)庫連接的方式
Web.config文件是一個XML文本文件,它用來儲存 ASP.NET Web 應(yīng)用程序的配置信息(如最常用的設(shè)置ASP.NET Web 應(yīng)用程序的身份驗證方式),它可以出現(xiàn)在應(yīng)用程序的每一個目錄中。本文主要介紹web.config中配置數(shù)據(jù)庫連接的兩種方式,一起來看。2015-10-10
Asp.NetCore3.1開源項目升級為.Net6.0的方法實現(xiàn)
自從.Net6.0出來后,一直想之前開發(fā)的項目升級.Net6.0,本文就詳細(xì)的介紹一下如何將Asp.NetCore3.1開源項目升級為.Net6.0,感興趣的小伙伴們可以參考一下2021-12-12
Asp.net GridView使用大全(分頁實現(xiàn))
關(guān)于GridView的使用涉及很多,網(wǎng)絡(luò)上零零散散的有一些,為了讓自己使用方便,也為了大家能很好的學(xué)習(xí)與工作,我把網(wǎng)絡(luò)上的GridView使用方法收集了一些2013-04-04
.Net Core實現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化
這篇文章主要介紹了.Net Core實現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
ASP.NET?Core中Razor頁面與MVC區(qū)別介紹
這篇文章介紹了ASP.NET?Core中Razor頁面與MVC的區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

