Entity Framework Core對(duì)Web項(xiàng)目生成數(shù)據(jù)庫表
一、引言
這篇文章中我們講解如何在Web項(xiàng)目中使用EntityFrameworkCore,并生成數(shù)據(jù)庫表,這里以ASP.NET Core WebApi為例講解。還是采用分層的結(jié)構(gòu)。創(chuàng)建后的項(xiàng)目整體結(jié)構(gòu)如下圖所示:
項(xiàng)目結(jié)構(gòu):
- EFCoreWeb.API:ASP.NET Core WebApi項(xiàng)目,用來提供Web功能,在項(xiàng)目中會(huì)引用EFCoreWeb.Data。
- EFCoreWeb.Data:類庫項(xiàng)目,基于.NET Core的類庫。存放的是與EFCore相關(guān)的操作。
- EFCoreWeb.Model:類庫項(xiàng)目,基于.NET Core的類庫。存放的是實(shí)體類。
1、添加實(shí)體類
我們?cè)贓FCoreWeb.Model類庫項(xiàng)目里面添加Student實(shí)體類:
namespace EFCoreWeb.Model { public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Gender { get; set; } } }
2、添加Mircosoft.EntityFrameworkCore
因?yàn)橐褂肧tudent實(shí)體類,首先要在EFCoreWeb.Data項(xiàng)目里面添加對(duì)EFCoreWeb.Model的引用。
然后在EFCoreWeb.Data類庫項(xiàng)目里面添加Mircosoft.EntityFrameworkCore包,直接在NuGet里面安裝:
由于我們使用的是SQLServer數(shù)據(jù)庫,所以我們還要安裝Microsoft.EntityFrameworkCore.sqlServer包,同樣也是直接在NuGet里面安裝:
安裝完上面的兩個(gè)包以后,在EFCoreWeb.Data類庫項(xiàng)目里面添加Mapping文件夾,用來存放Fluent API的配置文件,Student類的配置伙伴類代碼如下:
using EFCoreWeb.Model; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace EFCoreWeb.Data.Mapping { /// <summary> /// Student配置伙伴類,繼承自IEntityTypeConfiguration<T>泛型接口 /// </summary> public class StudentMap : IEntityTypeConfiguration<Student> { /// <summary> /// 實(shí)現(xiàn)接口里面的Configure方法,用來配置生成數(shù)據(jù)庫表結(jié)構(gòu) /// </summary> /// <param name="builder"></param> public void Configure(EntityTypeBuilder<Student> builder) { // 設(shè)置主鍵 builder.HasKey(p => p.Id); // 設(shè)置生成的表名 builder.ToTable("T_Student"); // 設(shè)置Name列的最大長(zhǎng)度 builder.Property("Name").HasMaxLength(64); // 設(shè)置Name列是必須的 builder.Property("Name").IsRequired(); } } }
添加一個(gè)Context文件夾,然后添加數(shù)據(jù)上下文類,繼承自DbContext:
using EFCoreWeb.Model; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace EFCoreWeb.Data.Mapping { /// <summary> /// Student配置伙伴類,繼承自IEntityTypeConfiguration<T>泛型接口 /// </summary> public class StudentMap : IEntityTypeConfiguration<Student> { /// <summary> /// 實(shí)現(xiàn)接口里面的Configure方法,用來配置生成數(shù)據(jù)庫表結(jié)構(gòu) /// </summary> /// <param name="builder"></param> public void Configure(EntityTypeBuilder<Student> builder) { // 設(shè)置主鍵 builder.HasKey(p => p.Id); // 設(shè)置生成的表名 builder.ToTable("T_Student"); // 設(shè)置Name列的最大長(zhǎng)度 builder.Property("Name").HasMaxLength(64); // 設(shè)置Name列是必須的 builder.Property("Name").IsRequired(); } } }
二、生成數(shù)據(jù)庫表
這里我們使用程序包管理器控制臺(tái)遷移的方式來生成數(shù)據(jù)庫表。需要在EFCoreWeb.Data項(xiàng)目里面安裝Microsoft.EntityFrameworkCore.Tools包。在EFCoreWeb.API項(xiàng)目里面安裝Microsoft.EntityFrameworkCore.Tools、Microsoft.EntityFrameworkCore兩個(gè)包。EFCoreWeb.API項(xiàng)目添加對(duì)EFCoreWeb.Data項(xiàng)目的引用。
首先在EFCoreWeb.API項(xiàng)目的appsettings.json文件里面添加數(shù)據(jù)庫連接字符串:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionString": { "DbConnection": "Data Source=.;Initial Catalog=EFTestDb;User ID=sa;Password=123456;" } }
在Startup類的ConfigureServices方法里面添加數(shù)據(jù)庫連接:
using EFCoreWeb.Data.Context; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace EFCoreWeb.API { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region 數(shù)據(jù)庫連接 services.AddDbContext<EFCoreDbContext>(options => { // options.UseSqlServer(Configuration.GetConnectionString("DbConnection")); options.UseSqlServer(Configuration.GetSection("ConnectionString").GetSection("DbConnection").Value); }); #endregion services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
上面步驟配置完成以后,在程序包管理器控制臺(tái)里面開始遷移,使用下面的命令添加遷移:
Add-Migration Init
如下圖所示:
執(zhí)行完命令以后就會(huì)生成遷移文件:
添加遷移之后,執(zhí)行下面的命令更新數(shù)據(jù)庫:
Update-Database
如下圖所示:
執(zhí)行完以后去查看數(shù)據(jù)庫:
可以看到,表里面Name列的長(zhǎng)度是根據(jù)代碼里面設(shè)置的長(zhǎng)度生成的,而且不為null,種子數(shù)據(jù)也插入進(jìn)去了。
到此這篇關(guān)于Entity Framework Core對(duì)Web項(xiàng)目生成數(shù)據(jù)庫表的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework?Core生成數(shù)據(jù)庫表
- Entity Framework Core使用控制臺(tái)程序生成數(shù)據(jù)庫表
- .NET Core使用EF生成數(shù)據(jù)庫出錯(cuò)的解決方法
- Entity Framework使用Code First模式管理數(shù)據(jù)庫
- 使用EF的Code?First模式操作數(shù)據(jù)庫
- EFCore 通過實(shí)體Model生成創(chuàng)建SQL Server數(shù)據(jù)庫表腳本
- Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫
相關(guān)文章
asp.net與excel互操作實(shí)現(xiàn)代碼
將datatable中的數(shù)據(jù)導(dǎo)出到指定的excel文件中2010-04-04ASP.NET MVC4 利用uploadify.js多文件上傳
本文主要介紹了ASP.NET MVC4利用uploadify.js實(shí)現(xiàn)多文件上傳的方法代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)
這篇文章介紹了ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10注冊(cè)表中存儲(chǔ)數(shù)據(jù)庫鏈接字符串的方法
2008-01-01asp.net(C#)遍歷memcached緩存對(duì)象
出于性能考慮,memcached沒有提供遍歷功能,不過我們可以通過以下兩個(gè)stats命令得到所有的緩存對(duì)象。2010-03-03ASP.NET 生成靜態(tài)頁面 實(shí)現(xiàn)思路
網(wǎng)上的cms系統(tǒng)好多都是支持生成靜態(tài)的,大家在使用過程中,也肯定遇到了很多的問題,下面就是一些實(shí)現(xiàn)的原理,其實(shí) asp,php,asp.net的原理都是差不多的。2009-06-06ASP.NET MVC HtmlHelper如何擴(kuò)展
ASP.NET MVC 中HtmlHelper方法為我們提供很多html標(biāo)簽,只需在頁面調(diào)用就行了,但是微軟并沒有把所有的html標(biāo)簽都對(duì)應(yīng)有了擴(kuò)展方法,需要我們自定義HtmlHelper,來滿足我們需要。2016-05-05