简体   繁体   English

Entity Framework Core - 从现有数据库创建模型后尝试添加迁移时出错

[英]Entity Framework Core - Error when trying to Add Migration after creating Model from existing database

I have .NET Core Project with Entity Framework Core.我有 .NET Core 项目和 Entity Framework Core。 I created a model from an existing database using the following command我使用以下命令从现有数据库创建了一个模型

Scaffold-DbContext "Server=.\\SQLEXPRESS;Database=DBName;Trusted_Connection=True;" Scaffold-DbContext "Server=.\\SQLEXPRESS;Database=DBName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型

The Model was generated and I tried to add migration but the migration that is generated is actually trying to create anew the already existing tables in the database.模型已生成,我尝试添加迁移,但生成的迁移实际上是在尝试重新创建数据库中现有的表。 So when I try to run Update-Database I naturally run into the following error因此,当我尝试运行 Update-Database 时,我自然会遇到以下错误

Applying migration '20200912095755_SetMaxLengthToBookTitle'.正在应用迁移“20200912095755_SetMaxLengthToBookTitle”。 Failed executing DbCommand (20ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [Authors] ( [Id] int NOT NULL IDENTITY, [Name] nvarchar(max) NULL, CONSTRAINT [PK_Authors] PRIMARY KEY ([Id]) );执行 DbCommand 失败 (20ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [Authors] ( [Id] int NOT NULL IDENTITY, [Name] nvarchar(max) NULL, CONSTRAINT [PK_Authors] ] 主键 ([Id]) ); Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Authors' in the database. Microsoft.Data.SqlClient.SqlException (0x80131904):数据库中已经有一个名为“Authors”的对象。 at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)在 Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException 异常,Boolean breakConnection,Action 1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) 在 Microsoft.Data.SqlClient.TdsParser .ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject) Boolean stateOb

What is the reason for that and what is the correct way to update database after creating the model from an already existing database in EF Core?这是什么原因,从 EF Core 中现有的数据库创建模型后更新数据库的正确方法是什么?

Basically the answer that I found for myself is that I have to add an Initial migration and delete everything in it to make an empty migration.基本上,我为自己找到的答案是我必须添加初始迁移并删除其中的所有内容以进行空迁移。 From then on I can proceed by using the code first approach as with the option 'Code first from database' that was available in EF 6.从那时起,我可以继续使用代码优先方法,就像 EF 6 中提供的“数据库中的代码优先”选项一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework Core 从现有数据库创建 model - Entity Framework Core creating model from existing database 实体框架核心中的添加迁移错误 - Add-Migration Error in Entity Framework Core 使用实体框架在 .NET Core 3.1 中进行迁移后,如何更新数据库 model? - How can I update the database model after doing a migration in .NET Core 3.1 using Entity Framework? 从Entity Framework中的现有表创建模型 - Creating model from existing table in Entity Framework Entity Framework Core - 尝试从迁移创建数据库,但遇到“Microsoft.Extensions.Hosting 服务”问题 - Entity Framework Core - trying to create a database from a migration but getting an issue with “Microsoft.Extensions.Hosting services” 实体框架尝试添加数据库中已有表的迁移 - Entity Framework trying to add migration of tables that are already in database 从具有空的MigrationHistory表的现有生产数据库迁移Entity Framework - Entity Framework migration from existing production database with empty MigrationHistory table 使用 Entity Framework Core 添加迁移时出现错误 - I get an error when I add migration using Entity Framework Core 在 Entity Framework Core 中初始迁移后出现“没有这样的表”错误 - “No such table” error after initial migration in Entity Framework Core 实体框架迁移 - 尝试添加种子数据时出现问题 - Entity Framework migration - issue when trying to add seed data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM