Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
在上一篇文章中講了如何使用fluent API來創(chuàng)建數(shù)據(jù)表,不知道你有沒有注意到一個問題。上面的OnModelCreating方法中,我們只配置了一個類Product,也許代碼不是很多,但也不算很少,如果我們有1000個類怎么辦?都寫在這一個方法中肯定不好維護。EF提供了另一種方式來解決這個問題,那就是為每個實體類單獨創(chuàng)建一個配置類。然后在OnModelCreating方法中調(diào)用這些配置伙伴類。
創(chuàng)建Product實體類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity.ModelConfiguration; namespace EF配置伙伴.Model { public class Product { public int ProductNo { get; set; } public string ProductName { get; set; } public double ProductPrice { get; set; } } }
創(chuàng)建Product實體類的配置類:ProductMap,配置類需要繼承自EntityTypeConfiguration泛型類,EntityTypeConfiguration位于System.Data.Entity.ModelConfiguration命名空間下,ProductMap類如下:
using EF配置伙伴.Model; using System; using System.Collections.Generic; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Text; namespace EF配置伙伴.EDM { public class ProductMap :EntityTypeConfiguration<Product> { public ProductMap() { // 設(shè)置生成的表名稱 ToTable("ProductConfiguration"); // 設(shè)置生成表的主鍵 this.HasKey(p => p.ProductNo); // 修改生成的列名 this.Property(p =>p.ProductNo).HasColumnName("Id"); this.Property(p => p.ProductName) .IsRequired() // 設(shè)置 ProductName列是必須的 .HasColumnName("Name"); // 將ProductName映射到數(shù)據(jù)表的Name列 } } }
在數(shù)據(jù)上下文Context類的OnModelCreating()方法中調(diào)用:
using EF配置伙伴.EDM; using EF配置伙伴.Model; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; namespace EF配置伙伴.EFContext { public class Context:DbContext { public Context() : base("DbConnection") { } public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 添加Product類的配置類 modelBuilder.Configurations.Add(new ProductMap()); base.OnModelCreating(modelBuilder); } } }
查看數(shù)據(jù)庫,可以看到符合我們的更改:
這種寫法和使用modelBuilder是幾乎一樣的,只不過這種方法更好組織處理多個實體。你可以看到上面的語法和寫jQuery的鏈式編程一樣,這種方式的鏈式寫法就叫Fluent API。
到此這篇關(guān)于Entity Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework使用Fluent?API配置案例
- Entity?Framework實現(xiàn)數(shù)據(jù)遷移
- Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)
- Entity Framework常用查詢語句
- Entity Framework中執(zhí)行sql語句
- Entity Framework系統(tǒng)架構(gòu)與原理介紹
- Entity?Framework?Core實現(xiàn)Like查詢詳解
- Entity Framework Core批處理SQL語句
- Entity Framework Core實現(xiàn)軟刪除與查詢過濾器
- Entity Framework Core生成列并跟蹤列記錄
- Entity?Framework實體拆分多個表
相關(guān)文章
ASP.NET動態(tài)加載用戶控件的實現(xiàn)方法
動態(tài)加載用戶控件的方法,用asp.net的朋友推薦2008-10-10ASP.NET中iframe框架點擊左邊頁面鏈接 右邊顯示鏈接頁面內(nèi)容
這篇文章主要介紹了ASP.NET中iframe框架點擊左邊頁面鏈接,右邊顯示鏈接頁面內(nèi)容的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-07-07asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)
本篇文章主要是對asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02ASP.NET Core中修改配置文件后自動加載新配置的方法詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中修改配置文件后自動加載新配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習或者使用ASP.NET Core具有一定的參考學(xué)習價值,需要的朋友們下面來一起學(xué)習學(xué)習吧2020-08-08ASP.NET 連接ACCESS數(shù)據(jù)庫的簡單方法
一段非常簡單的連接ACCESS數(shù)據(jù)庫的實例代碼,有需要的朋友可以參考一下2013-07-07淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結(jié)
這篇文章主要介紹了淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結(jié),以便在實際的開發(fā)中選用合適的控件進行數(shù)據(jù)綁定,以提高開發(fā)效率。2016-12-12