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

ef core migrations 创建新的迁移程序

时间:2024-10-17 21:33:25浏览次数:11  
标签: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依赖也是一样的,但是当依赖文件中的内容发生改动的时候,两种依赖就会产生差别:对于普通依赖而言,当依赖发生改变需要重新与目标文件生成链接,也就是说如果任......
  • EF4323 Algorithmic Trading
    EF4323AlgorithmicTradingProjectSemesterA2024/2025Instructor:Dr.DU,JintaoIntroductionInthisproject,wesimulatethealgorithmictradingbusinessmodel,whereasmallgroupofproprietarytraderstriestoattractoutsideinstitutionalinvest......
  • Citrix NetscalerStoreFront负载均衡 Load Balancing(精华)
    CitrixNetscalerStoreFront负载均衡LoadBalancing(精华)1.依次展开TrafficManagement/LoadBalancing/Servers,点add添加 在此,我的两台storefront已经全部添加进去4.依次展开TrafficManagement/LoadBalancing/Monitors,点击add添加 6.依次展开TrafficMa......
  • ContentSizeFitter
    新建在物体的Inspector面板中添加组件(AddComponent->ContentSizeFitter)演示介绍ContentSizeFitter内容大小适配器内容大小适配器充当布局控制器,可用于控制其自身布局元素的大小。查看实际自动布局系统的最简单方法是向带有文本组件的游戏对象添加内容大小适配器......
  • Codeforces Round 924 (Div. 2) D. Lonely Mountain Dungeons(推式子,思维,差分,前缀和)
    题目链接CodeforcesRound924(Div.2)D.LonelyMountainDungeons思路令f(n,m......
  • Storefront与NetScaler的集成配置 - part2
    Storefront与NetScaler的集成配置-part2前文介绍了Storefront与NetScaler配置中的StoreFront方面的配置,本章将介绍NetScaler部分的配置。1.从download.citrix.com官方网站下载最新的NetScalerGateway的。对于StoreFront来说,NetSclaer最好使用10.0e和10.1的版本(9.2不支持)。本......
  • C语言typedef常用方法
    在C语言中,typedef关键字可以用来为已有的数据类型定义一个新的标识符,它的常用方法有:typedef的4种常见用法:一、给已定义的变量类型起个别名二、定义函数指针类型三、定义数组指针类型四、定义数组类型总结一句话:“加不加typedef,类型是一样的“,这句话可以这样理解:没加type......
  • 新建next.js项目,customize the default import alias是否要自定义默认导入别名(@/*)的选
    使用命令npxcreate-next-app@latest新建项目时,会自定义一些选项,如下图:其中自定义导入别名的选项,选择Yes或No有何区别?Wouldyouliketocustomizethedefaultimportalias(@/*)?...No/Yes一、选择"Yes"jsconfig.js文件的内容是:{"compilerOptions":{......
  • CMSC Manual testing Completeness SNU Score
    Homework#3Due:Friday,October18that4:00pmCSTTableofContentsHomework#3GettingstartedManualtestingCompletenessSNUScoreCodeQualitySubmissionThepurposeofthisassignmentistogiveyouexperiencewithconditionals,lists,andloops.......
  • Codeforces Beta Round 93 (Div. 1 Only) B. Password 一题三吃
    https://codeforces.com/problemset/problem/126/B学完Z函数,先用哈希做了一遍,再用Z函数做了一遍,然后搜其他人的题解发现用next数组也能做,就又做了一遍B.Password题意给一串字符串\(s\),要求找一个最长的\(t\)\(t\)既是\(s\)的前缀串,也是后缀串,还是除了前缀后缀外的一个......