首页 > 编程语言 >ef core migrations 创建新的迁移程序

ef core migrations 创建新的迁移程序

时间:2024-10-17 21:33:25浏览次数:17  
标签:core builder ef migrations var using Configuration GetConnectionString options

EF Core Migrations

创建一个WebAPI.Migrations

dotnet new webapi -n WebAPI.Migrations

Program.cs

using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using DataAccess;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<MusicCollectionDb>(
    options =>options.UseSqlServer(builder.Configuration.GetConnectionString("MusicDbConnection"),b => b.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName)));

var app = builder.Build();
app.UseAuthorization();
app.MapControllers();
app.Run();

其他数据库

var msSqlConnection = builder.Configuration.GetConnectionString("MsSqlConnection");
options.UseSqlServer(msSqlConnection, b => b.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName));

var sqliteConnection = builder.Configuration.GetConnectionString("SqliteConnection");
options.UseSqlite(sqliteConnection, b => b.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName));

var mySqlConnection = builder.Configuration.GetConnectionString("MySqlConnection");
//MySql需要传入版本,ServerVersion.AutoDetect根据连接字符串自动获取
options.UseMySql(mySqlConnection, ServerVersion.AutoDetect(mySqlConnection), b => b.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName));

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MusicDbConnection": "Data Source=localhost;Initial Catalog=musicdb2;User ID=sa;Password=song2030..;Trust Server Certificate=True"
  }
}

添加迁移文件

dotnet ef migraitons add init 
dotnet ef database update

标签:core,builder,ef,migrations,var,using,Configuration,GetConnectionString,options
From: https://www.cnblogs.com/zhuoss/p/18473148

相关文章

  • Makefile入门学习过程中的一些知识点-一些常见规则或语法:
    1.order-only依赖:还是以上一篇的sudoku项目为例,之前写的目标之后的依赖都属于普通依赖,普通依赖都对应自身的规则,order-only依赖也是一样的,但是当依赖文件中的内容发生改动的时候,两种依赖就会产生差别:对于普通依赖而言,当依赖发生改变需要重新与目标文件生成链接,也就是说如果任......