将迁移与 GenericContext 结合使用
Using Migrations with GenericContext
我正在尝试使用迁移,但遇到以下问题
$ dotnet ef migrations add InitialMigration
No DbContext was found in assembly 'VirtualStore.Data'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
这里是我的语境class
using Microsoft.EntityFrameworkCore;
using VirtualStore.Mapping.Mapping;
namespace VirtualStore.Data
{
public class Context<T> : DbContext where T : Entity
{
public DbSet<T> Entity { get; set; }
public Context()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=DESKTOP-3UEM3PC;Initial Catalog=VIRTUALSTORE;Integrated Security=SSPI;");
base.OnConfiguring(optionsBuilder);
}
}
}
还有我的解决方案
任何人都知道我该怎么做才能将迁移与此架构一起使用?
您不希望您的 DbContext "Context" 是通用的。使 "Context" 不是通用的,并为映射到数据库的每个实体类型包含单独的 DbSet 属性。
https://docs.microsoft.com/en-us/ef/core/modeling/included-types
为什么要创建通用 DbContext?通过查看您的代码,您将自己限制为每个 DbContext 创建一个实体。例如,假设您必须创建 Sale 和相关的 SaleItem。您将不得不实例化两个 DbContext 对象而不是一个...这会带来一整套问题。
我的建议不是创建通用 DbContext,而是使 DbSet 通用。有几种方法可以做到这一点,但 here 是我最近做过的一个例子。
另请注意,在此解决方案中,我必须使用项目和项目源开关:
dotnet ef migrations add InitialMigration -p ..\Freelanceme.Data -s ..\Freelanceme.WebApi
我能够像这样包装我输入的上下文:
public class CasbinDbContextTypeWrapper : CasbinDbContext<int>
{
public CasbinDbContextTypeWrapper(DbContextOptions<CasbinDbContextTypeWrapper> options)
: base(options)
{
// Intentionally empty
}
}
我正在尝试使用迁移,但遇到以下问题
$ dotnet ef migrations add InitialMigration
No DbContext was found in assembly 'VirtualStore.Data'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
这里是我的语境class
using Microsoft.EntityFrameworkCore;
using VirtualStore.Mapping.Mapping;
namespace VirtualStore.Data
{
public class Context<T> : DbContext where T : Entity
{
public DbSet<T> Entity { get; set; }
public Context()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=DESKTOP-3UEM3PC;Initial Catalog=VIRTUALSTORE;Integrated Security=SSPI;");
base.OnConfiguring(optionsBuilder);
}
}
}
还有我的解决方案
任何人都知道我该怎么做才能将迁移与此架构一起使用?
您不希望您的 DbContext "Context" 是通用的。使 "Context" 不是通用的,并为映射到数据库的每个实体类型包含单独的 DbSet 属性。
https://docs.microsoft.com/en-us/ef/core/modeling/included-types
为什么要创建通用 DbContext?通过查看您的代码,您将自己限制为每个 DbContext 创建一个实体。例如,假设您必须创建 Sale 和相关的 SaleItem。您将不得不实例化两个 DbContext 对象而不是一个...这会带来一整套问题。
我的建议不是创建通用 DbContext,而是使 DbSet 通用。有几种方法可以做到这一点,但 here 是我最近做过的一个例子。
另请注意,在此解决方案中,我必须使用项目和项目源开关:
dotnet ef migrations add InitialMigration -p ..\Freelanceme.Data -s ..\Freelanceme.WebApi
我能够像这样包装我输入的上下文:
public class CasbinDbContextTypeWrapper : CasbinDbContext<int>
{
public CasbinDbContextTypeWrapper(DbContextOptions<CasbinDbContextTypeWrapper> options)
: base(options)
{
// Intentionally empty
}
}