我的代码很少,在创建数据迁移时出现此错误。
PS C:\Users\username\source\repos\ToDoAPI\ToDoAPI> dotnet ef migrations add initialmigration
Build started...
Build succeeded.
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.String]' to type 'System.String'.
at Microsoft.EntityFrameworkCore.Migrations.Internal.SnapshotModelProcessor.<>c.<.ctor>b__3_1(FieldInfo p)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
at Microsoft.EntityFrameworkCore.Migrations.Internal.SnapshotModelProcessor..ctor(IOperationReporter operationReporter, IModelRuntimeInitializer modelRuntimeInitializer)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
程序.cs
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));
var app = builder.Build();
待办事项.cs
public class ToDo
{
[Key]
public string Id { get; set; }
public string? ToDoName { get; set; }
}
数据
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<ToDo> ToDos => Set<ToDo>();
}
我不知道这个转换发生在哪里,从而产生构建错误。
我也有同样的错误。
将
Microsoft.Data.SqlClient
升级到版本 5.2.0
,并将 Microsoft.EntityFrameworkCore.SqlServer
从 9.0.0-preview.1.24081.2
降级到 8.0.2
。还将 dotnet-ef
工具从 8.0.2
更新为 7.0.3
。
希望对您有帮助。