首页 > 学院 > 开发设计 > 正文

ASP.Net Core DB Migration

2019-11-08 02:53:41
字体:
来源:转载
供稿:网友
Conceptasp.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: VS2015Steps:1, check connectionstringappsettings.json{  "ConnectionStrings": {    "DefaultConnection":"Server=(localdb)//mssqllocaldb;Database=ContosoUniversity2;Trusted_Connection=True;MultipleActiveResultSets=true"  } 2,  add/del fieldModels: add/del PRopertyMvc/Views:  add/del show field 3, DB MigrationA, SolutionExplorer/right-click the project/Open in File Explorer B, Hold down theShift key/right-click the project/Open command window hereC, 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.csModels:存放表结构对应的类文件,VS由此创建数据库和迁移文件
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表