System.InvalidCastException: Unable to cast object of type ‘System.String’ to type ‘System.Guid’ 问题解决方法

當前文章的短網址連結為: https://unos.top/74v3

PM> Update-Database
Build started...
Build succeeded.
System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLStringTypeMapping.GenerateNonNullSqlLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.GenerateProviderValueSqlLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendSqlLiteral(StringBuilder commandStringBuilder, ColumnModification modification, String tableName, String schema)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.<>c.<AppendValues>b__20_0(StringBuilder sb, ColumnModification o, ValueTuple`3 p)
   at System.Text.StringBuilderExtensions.AppendJoin[T,TParam](StringBuilder stringBuilder, IEnumerable`1 values, TParam param, Action`3 joinAction, String separator)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendValues(StringBuilder commandStringBuilder, String name, String schema, IReadOnlyList`1 operations)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendInsertCommand(StringBuilder commandStringBuilder, String name, String schema, IReadOnlyList`1 writeOperations)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendInsertOperation(StringBuilder commandStringBuilder, ModificationCommand command, Int32 commandPosition)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(InsertDataOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__83_28(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at MySql.EntityFrameworkCore.Migrations.MySQLMigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to cast object of type 'System.Guid' to type 'System.String'.
PM> 

或者如下错误

gPM> Update-Database
Build started...
Build succeeded.
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Guid'.
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteGuidTypeMapping.GenerateNonNullSqlLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.GenerateProviderValueSqlLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendSqlLiteral(StringBuilder commandStringBuilder, ColumnModification modification, String tableName, String schema)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendWhereCondition(StringBuilder commandStringBuilder, ColumnModification columnModification, Boolean useOriginalValue)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.<AppendWhereClause>b__21_0(StringBuilder sb, ColumnModification v)
   at System.Text.StringBuilderExtensions.AppendJoin[T](StringBuilder stringBuilder, IEnumerable`1 values, Action`2 joinAction, String separator)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendWhereClause(StringBuilder commandStringBuilder, IReadOnlyList`1 operations)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendDeleteCommand(StringBuilder commandStringBuilder, String name, String schema, IReadOnlyList`1 conditionOperations)
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendDeleteOperation(StringBuilder commandStringBuilder, ModificationCommand command, Int32 commandPosition)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(DeleteDataOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__83_29(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.SqliteMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to cast object of type 'System.String' to type 'System.Guid'.
PM> 

我在ASP.NET Core中进行数据迁移Migration时,出现上述的问题,其实原因很简单,当时我正在切换数据库,用了另一个数据库的seed, 因此,肯定会有问题。

所以,更换数据库时,一定要重新生成seed文件迁移。

5 1 投票
文章评分
订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

3 评论
最旧
最新 最多投票
内联反馈
查看所有评论
天堂的魔鬼

感谢分享

无名氏

您听说过吗?谢谢。

无名氏

太可怕了,难以相信??