标签:get rtu tco source move hosts 异常 默认 tools
编译器:VS2019
数据库:SqlServer 2019
DotNet Core SDK(3.1.400)
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
EntityFramework
Microsoft.EntityFrameworkCore.Design
1.创建第一个迁移
Add-Migration InitialCreate
2.创建数据库和架构
Update-Database
如果在实体中需要新增CreatedTimestamp
字段
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedTimestamp { get; set; }
}
执行如下命令创建新迁移:
Add-Migration AddBlogCreatedTimestamp
Update-Database
如果执行Update-Database
异常需要删除上一个添加的迁移命令
删除上一个添加的迁移命令
Remove-Migration
新增ConnectionString
节点
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionString": {
"SqlServer": "server=.;database=NetCoreDemo;uid=sa;pwd=123"
}
}
public void ConfigureServices(IServiceCollection services)
{
string constr = Configuration.GetSection("ConnectionString:SqlServer").Value;
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(constr));
services.AddControllersWithViews();
}
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
MySql.Data.EntityFrameworkCore
新增ConnectionString
节点
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionString": {
"MySql": "Data Source=localhost;port=3306;database=NetCoreDemo;User Id=root;Password=12345"
}
}
public void ConfigureServices(IServiceCollection services)
{
var constr = Configuration.GetSection("ConnectionString:MySql").Value;
services.AddDbContext<MyDbContext>(
options => options.UseMySQL(constr)
);
services.AddControllersWithViews();
}
在依次执行上述迁移命令即可
注意执行命令时必须默认项目必须选择继承了DbContext 的那个程序集
参考地址:https://docs.microsoft.com/zh-cn/ef/core/managing-schemas/migrations/?tabs=vs
DotNet Core 3.1 EF Core 数据库迁移(Migration)
标签:get rtu tco source move hosts 异常 默认 tools
原文地址:https://www.cnblogs.com/imtudou/p/13664068.html