[英]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,Action1 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.