本节目标
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(); }); } } }