C# ASP.NET Core InvalidOperationException:尝试激活UserStore时无法解析DbContext类型的服务
我的应用程序中有这个DbContext,它使用IdentityDbContextC# ASP.NET Core InvalidOperationException:尝试激活UserStore时无法解析DbContext类型的服务,c#,asp.net-core,entity-framework-core,dbcontext,C#,Asp.net Core,Entity Framework Core,Dbcontext,我的应用程序中有这个DbContext,它使用IdentityDbContext public class ShopDbContext : IdentityDbContext<User>, IDisposable { public ShopDbContext(DbContextOptions options): base(options) { Products = base.Set<Pr
public class ShopDbContext : IdentityDbContext<User>, IDisposable
{
public ShopDbContext(DbContextOptions options): base(options)
{
Products = base.Set<Product>();
Categories = base.Set<Category>();
Shops = base.Set<Shop>();
Properties = base.Set<Property>();
Orders = base.Set<Order>();
OrderItems = base.Set<OrderItem>();
Customers = base.Set<Customer>();
Carts = base.Set<Cart>();
CartItems = base.Set<CartItem>();
UserShops = base.Set<UserShop>();
Images = base.Set<Image>();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Use singular table names
modelBuilder.ApplyConfiguration<Product>(new ProductConfiguration());
modelBuilder.ApplyConfiguration<Category>(new CategoryConfiguration());
modelBuilder.ApplyConfiguration<Shop>(new ShopConfiguration());
modelBuilder.ApplyConfiguration<Property>(new PropertyConfiguration());
modelBuilder.ApplyConfiguration<Order>(new OrderConfiguration());
modelBuilder.ApplyConfiguration<OrderItem>(new OrderItemConfiguration());
modelBuilder.ApplyConfiguration<Customer>(new CustomerConfiguration());
modelBuilder.ApplyConfiguration<Cart>(new CartConfiguration());
modelBuilder.ApplyConfiguration<CartItem>(new CartItemConfiguration());
modelBuilder.ApplyConfiguration<UserShop>(new UserShopConfiguration());
modelBuilder.ApplyConfiguration<Image>(new ImageConfiguration());
}
public DbSet<Shop> Shops { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Property> Properties { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Cart> Carts { get; set; }
public DbSet<CartItem> CartItems { get; set; }
public DbSet<UserShop> UserShops { get; set; }
public DbSet<Image> Images { get; set; }
}
我认为问题在于我设置DbContextFactory的方式,但是当我遵循它提供的示例时,我使用了相同的方法
此外,我的连接字符串是:
"ConnectionStrings": {
"DefaultConnection": "User ID=sa; Password=123456;Server=.\\sqlexpress;Database=ShopDb;"
}
当我尝试使用Package Manager update database生成数据库时,出现以下错误:
Format of the initialization string does not conform to specification starting at index 0.
试着换成这个
public ShopDbContext(DbContextOptions<ShopDbContext> options): base(options)
公共ShopDbContext(DbContextOptions):基本(选项)
并改变它
services.AddDbContextFactory<ShopDbContext>(builder =>
builder.UseSqlServer("DefaultConnection"));
services.AddDbContextFactory(生成器=>
builder.UseSqlServer(“默认连接”);
对此
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextFactory<DbContext,ShopDbContext>(builder =>
builder.UseSqlServer(connectionString));
stringconnectionstring=Configuration.GetConnectionString(“DefaultConnection”);
services.AddDbContextFactory(生成器=>
UseSqlServer(connectionString));
试着换成这个
public ShopDbContext(DbContextOptions<ShopDbContext> options): base(options)
公共ShopDbContext(DbContextOptions):基本(选项)
并改变它
services.AddDbContextFactory<ShopDbContext>(builder =>
builder.UseSqlServer("DefaultConnection"));
services.AddDbContextFactory(生成器=>
builder.UseSqlServer(“默认连接”);
对此
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextFactory<DbContext,ShopDbContext>(builder =>
builder.UseSqlServer(connectionString));
stringconnectionstring=Configuration.GetConnectionString(“DefaultConnection”);
services.AddDbContextFactory(生成器=>
UseSqlServer(connectionString));
Dudu,您传入dbContext而不是connectionString,如“User ID=sa;Password=123456;Server=.\\sqlexpress;Database=ShopDb;”,您只传入“DefaultConnection”(它不是connectionString,只是appSettings文件中的一部分)Dudu,您传入dbContext而不是connectionString“用户ID=sa;密码=123456;服务器=。\\sqlexpress;数据库=ShopDb;,您只需传递“DefaultConnection”(它不是connectionString,只是appSettings文件中的部分)
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextFactory<DbContext,ShopDbContext>(builder =>
builder.UseSqlServer(connectionString));