.NET6 EFCore & Cosmos移民问题。需要帮助。
大家好啊。我是.Net的新手,我正面临一个谷歌未能帮助我解决的问题。你是我的最后一任。
所以。我正在尝试使用HomeControlCenter 6.0.3从我的小EFCore项目连接到Azure Cosmos DB
错误:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext>
object in its constructor and passes it to the base constructor for DbContext.我的Program.cs:
builder.Services.AddDbContext<ControlCenterContext>(options =>
options.UseCosmos(builder.Configuration.GetConnectionString("DefaultConnection"), "ToDoList"));我的DbContext冲动:
public class ControlCenterContext : DbContext
{
public ControlCenterContext(DbContextOptions<ControlCenterContext> options) : base(options)
{
}
}我还尝试使用OnConfiguring的重写,而不是Program.cs行。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseCosmos(
"<AccountEndpoint>",
"<accountKey>",
databaseName: "ToDoList");什么都帮不上忙。当我运行dotnet ef migrations add "initialSetup"时,我会得到上面提到的错误。
我仔细阅读了错误,正如您所看到的,我确实应用了所有必要的构造函数参数&其他添加.我甚至试图创造一个香草的项目,并再次做同样的事.
发布于 2022-07-14 17:20:06
我找不到微软的任何官方消息,但是这个博客的作者说使用的迁移是不支持的:https://www.thereformedprogrammer.net/an-in-depth-study-of-cosmos-db-and-ef-core-3-0-database-provider/#1-no-migrations-can-cause-problems
这是有意义的,因为CosmosDB是一个文档数据库,所以它没有模式,它只是一堆JSON文件。当我想使用迁移来创建种子数据时,我遇到了这个问题。我唯一能想到的解决方案是创建一个单独的项目,以静态值上传种子数据。但是,这只是种子数据,而不是模式更新。
发布于 2022-04-08 13:12:54
这个问题有点荒谬。Cosmos没有模式,因此迁移可能不受支持,也不需要。
https://stackoverflow.com/questions/71795255
复制相似问题