ASP.Net Core DB Migration

Concept
ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as web apps, IoT apps and mobile backends. ASP.NET Core apps can run on .NET Core or on the full .NET Framework.
ASP.NET Core: 新的支持云开发的跨平台,开源框架。可用来开发web app, 物联网, 手机后端
Source: GitHub 
Install: VS2015

Steps:
1, check connectionstring
appsettings.json
{
  "ConnectionStrings": {
    "DefaultConnection":"Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity2;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
 
2,  add/del field
Models: add/del property
Mvc/Views:  add/del show field
 
3, DB Migration
A, SolutionExplorer/right-click the project/Open in File Explorer 
B, Hold down theShift key/right-click the project/Open command window here
C, dotnet ef migrations add [RMProductName] -c [Context name]
D, dotnet ef database update
 
Get: Data/Migrations/20170217100804_RMProductName.cs and Snapshot modifies by VS 
 
小结: 
A, Problem: There is already an object named 'CertificationKinds' in the database.
原因:__EFMigrationsHistory内沒有数据,VS就认为此时数据库内没有表,会自动根据Migrations文件夹内的迁移文件创建表结构。此时数据库内如果已经存在表CertificationKinds,就会产生报错。
解决:1,dotnet ef migrations add name :直接用修改过表结构的代码(根据:Model内对应的类),VS会根据迁移文件自动在__EFMigrationsHistory创建修改记录。
 dotnet ef database update :根据迁移文件更新本地数据库及表 __EFMigrationsHistory
 2,dotnet ef database update: 根据之前的迁移文件更新本地数据库及表 __EFMigrationsHistory
  dotnet ef migrations add name: 根据新的Model类产生迁移文件
  dotnet ef database update: 根据迁移文件更新本地数据库及表__EFMigrationsHistory
 
B, VS通过文件控制数据库修改
Data:数据库
Migrations:迁移文件夹,存放数据库迁移文件
迁移文件:20170217100804_RMProductName.cs数据库表结构修改记录,VS通过这些文件进行数据库表结构控制
xxxxModelSnapShot:AzureGalleryDbContextModelSnapshot.cs数据快照,数据库对象明细
xxxxContext:AzureGalleryDbContext.cs
Models:存放表结构对应的类文件,VS由此创建数据库和迁移文件ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as web apps, IoT apps and mobile backends. ASP.NET Core apps can run on .NET Core or on the full .NET Framework.
ASP.NET Core: 新的支持云开发的跨平台,开源框架。可用来开发web app, 物联网, 手机后端
Source: GitHub 
Install: VS2015

Steps:
1, check connectionstring
appsettings.json
{
  "ConnectionStrings": {
    "DefaultConnection":"Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity2;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
 
2,  add/del field
Models: add/del property
Mvc/Views:  add/del show field
 
3, DB Migration
A, SolutionExplorer/right-click the project/Open in File Explorer 
B, Hold down theShift key/right-click the project/Open command window here
C, dotnet ef migrations add [RMProductName] -c [Context name]
D, dotnet ef database update
 
Get: Data/Migrations/20170217100804_RMProductName.cs and Snapshot modifies by VS 
 
小结: 
A, Problem: There is already an object named 'CertificationKinds' in the database.
原因:__EFMigrationsHistory内沒有数据,VS就认为此时数据库内没有表,会自动根据Migrations文件夹内的迁移文件创建表结构。此时数据库内如果已经存在表CertificationKinds,就会产生报错。
解决:1,dotnet ef migrations add name :直接用修改过表结构的代码(根据:Model内对应的类),VS会根据迁移文件自动在__EFMigrationsHistory创建修改记录。
 dotnet ef database update :根据迁移文件更新本地数据库及表 __EFMigrationsHistory
 2,dotnet ef database update: 根据之前的迁移文件更新本地数据库及表 __EFMigrationsHistory
  dotnet ef migrations add name: 根据新的Model类产生迁移文件
  dotnet ef database update: 根据迁移文件更新本地数据库及表__EFMigrationsHistory
 
B, VS通过文件控制数据库修改
Data:数据库
Migrations:迁移文件夹,存放数据库迁移文件
迁移文件:20170217100804_RMProductName.cs数据库表结构修改记录,VS通过这些文件进行数据库表结构控制
xxxxModelSnapShot:AzureGalleryDbContextModelSnapshot.cs数据快照,数据库对象明细
xxxxContext:AzureGalleryDbContext.cs
Models:存放表结构对应的类文件,VS由此创建数据库和迁移文件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值