.net core 在GenericContext中使用迁移

.net core 在GenericContext中使用迁移,.net-core,entity-framework-core,entity-framework-migrations,.net Core,Entity Framework Core,Entity Framework Migrations,我正在尝试使用迁移,但我有以下问题 $ 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. 这里是我的上下文类 using Microsoft.EntityFr

我正在尝试使用迁移,但我有以下问题

$ 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.
这里是我的上下文类

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);
        }
    }
}
使用Microsoft.EntityFrameworkCore;
使用VirtualStore.Mapping.Mapping;
命名空间VirtualStore.Data
{
公共类上下文:DbContext,其中T:Entity
{
公共数据库集实体{get;set;}
公共上下文()
{
数据库。请重新创建();
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(“数据源=DESKTOP-3UEM3PC;初始目录=VIRTUALSTORE;集成安全性=SSPI;”);
基本配置(选项生成器);
}
}
}
我的解决方案呢


有人知道我能做什么,在这种体系结构中使用迁移吗?

您不希望您的DbContext“Context”是通用的。使“上下文”不是泛型的,并为映射到数据库的每个实体类型包含单独的DbSet属性


您不希望DbContext“Context”是泛型的。使“上下文”不是泛型的,并为映射到数据库的每个实体类型包含单独的DbSet属性


为什么要创建通用DbContext?通过查看代码,您将自己限制为每个DbContext创建一个实体。例如,假设您必须创建Sale和相关SaleItem。您必须实例化两个DbContext对象,而不是一个。。。这就带来了一系列的问题

我的建议不是创建泛型DbContext,而是将DbSet设置为泛型。有几种方法可以做到这一点,但这是我最近做的一个例子

还要注意,在这个解决方案中,我必须使用project和project source开关:

dotnet ef migrations add InitialMigration -p ..\Freelanceme.Data -s ..\Freelanceme.WebApi

为什么要创建泛型DbContext?通过查看代码,您将自己限制为每个DbContext创建一个实体。例如,假设您必须创建Sale和相关SaleItem。您必须实例化两个DbContext对象,而不是一个。。。这就带来了一系列的问题

我的建议不是创建泛型DbContext,而是将DbSet设置为泛型。有几种方法可以做到这一点,但这是我最近做的一个例子

还要注意,在这个解决方案中,我必须使用project和project source开关:

dotnet ef migrations add InitialMigration -p ..\Freelanceme.Data -s ..\Freelanceme.WebApi

异常消息告诉您在程序集“VirtualStore.Data”中未找到DbContext。确保使用的程序集正确,并且类型既不是抽象的也不是泛型的。我看不出您对我们有什么期望-实现一些不受支持的东西?异常消息告诉您在程序集“VirtualStore.Data”中找不到DbContext。确保使用的程序集正确,并且类型既不是抽象的也不是泛型的。我看不出你对我们有什么期望——实施一些不受支持的东西?