本節目標
1 創建實體類Entity
2 創建數據庫操作上下文DbContext
3 數據庫遷移
實戰內容
基於AspNetCore3.1
數據庫:Sqlserver2014
開發工具:Vs2019
解決方案:gitee上Xwy.WebApiDemo
項目:Xwy.Domain
依賴庫:Microsoft.EntityFrameworkCore,Microsoft.EntityFrameworkCore.SqlServer,Microsoft.EntityFrameworkCore.Tools,Microsoft.EntityFrameworkCore.Design
代碼
項目結構
依賴項都要安裝,兩個項目都要
Install-Package Microsoft.EntityFrameworkCore Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.Design
一個數據庫上下文對象時
add-migration init
update-database
多個數據庫上下文對象時
add-migration init -Context VueShopDbContext update-database -Context VueShopDbContext
數據庫上下文類
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using Xwy.Domain.Entities; namespace Xwy.Domain.DbContexts { public class VueShopDbContext:DbContext { public VueShopDbContext() { } public VueShopDbContext(DbContextOptions<VueShopDbContext> options):base(options) { } // 放入可以使用的數據庫對象 public DbSet<TestRecord> TestRecords { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer("Data Source=.;database=VueShopDb;uid=sa;pwd=123456"); //optionsBuilder.UseSqlServer("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.");//數據庫連接字符串,其中TestDB是數據庫名稱 } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<TestRecord>().ToTable("TestRecords","xwy"); base.OnModelCreating(modelBuilder); } } }
實體類
using System; using System.Collections.Generic; using System.Text; namespace Xwy.Domain.Entities { public class TestRecord { public int Id { get; set; } public string Name { get; set; } public string Title { get; set; } } }
啟動項目配置代碼
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Xwy.Domain.DbContexts; namespace Xwy.WebApiDemo { 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) { services.AddDbContext<ClothersMallDbContext>(m=> { }); services.AddSwaggerGen(m => { m.SwaggerDoc("v1",new OpenApiInfo() { Title="swaggertest",Version="v1"}); }); services.AddCors(m => m.AddPolicy("any", a => a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); 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.UseSwagger(); app.UseCors(); app.UseSwaggerUI(m=> { m.SwaggerEndpoint("/swagger/v1/swagger.json","swaggertest"); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }