无法为“模型"创建DbSet,因为此类型不包含在上下文的模型中 [英] Cannot create a DbSet for 'Model' because this type is not included in the model for the context

查看:109
本文介绍了无法为“模型"创建DbSet,因为此类型不包含在上下文的模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个泛型并使用DI

I do a Generic and using DI

所以我创建了一个空的类

so I create a empty class

public class DBRepo
{ 
}

和我的模型类继承了DBRepo类

and my model class to inheriting class DBRepo

public partial class UserAccount : DBRepo
{
   public int Id { get; set; }
   public string Account { get; set; }
   public string Pwd { get; set; }
}

然后这是一个做CRUD的接口

then this is a Interface to do CRUD

    public interface IDBAction<TEntity> where TEntity : class,new()
    {   
      void UpdateData(TEntity _entity);
      void GetAllData(TEntity _entity);
    }
public class DBService<TEntity> : IDBAction<TEntity> where TEntity : class,new()
{
    private readonly CoreContext _db;
    public DBService(CoreContext _db)
    {
        this._db = _db;
    }
    public void UpdateData(TEntity _entity)
    {
        this._db.Set<TEntity>().UpdateRange(_entity);
        this._db.SaveChanges();
    }
    public void GetAllData(TEntity _entity)
    {
        var x = this._db.Set<TEntity>().Select(o => o).ToList();
    }
}

构造函数中的依赖注入服务提供者

And I Dependency Injection Service Provider in constructor

this.DBProvider = new ServiceCollection()
    .AddScoped<IDBAction<DBRepo>, DBService<DBRepo>>()
    .AddScoped<DBContext>()
    .AddDbContext<CoreContext>(options => options.UseSqlServer(ConnectionString))
    .BuildServiceProvider();

我获得服务的最后一步

DBProvider.GetService<IDBAction<DBRepo>>().GetAllData(new UserAccount());

我将收到一条与标题相同的错误消息

I will get a error message same with title

或者我更改为

DBProvider.GetService<IDBAction<UserAccount>>().GetAllData(new UserAccount());

我会收到其他消息

对象引用未设置为对象的实例.'

Object reference not set to an instance of an object.'

但无效的UpdateData()可以工作,那么如何解决GetAllData()问题呢?

but the void UpdateData() is can work, so how to fix GetAllData() problem?

推荐答案

错误仅是因为您在此处使用的类 UserAccount 显然未添加到您的上下文 CoreContext中.那里应该有一个属性,例如:

The error simply is because the class you're using here UserAccount has apparently not been added to your context, CoreContext. There should be a property there like:

public DbSet<UserAccount> UserAccounts { get; set; }

无论是否最终使用通用的 Set< T> 访问器,仍必须为上下文中的实体定义 DbSet .

Regardless of whether you end up using the generic Set<T> accessor, you still must defined a DbSet for the entity on your context.

也就是说,您绝对不应该在回购中创建自己的服务集合.在Startup.cs的主要服务集合中注册您的上下文和您的回购,然后在需要的地方简单地注入您的回购.只要您有一个使用上下文的构造器(您看上去应该如此),DI框架就会使用您的上下文实例化它.

That said, you should absolutely not be creating your own service collection inside your repo. Register your context and your repo with the main service collection in Startup.cs and then simply inject your repo where you need it. The DI framework will take care of instantiating it with your context, as long as you have a constructor that takes your context (which you seem to).

然后那个说,您应该完全放弃该存储库.它仍然需要对Entity Framework的依赖,并且除了代理Entity Framework方法外,不执行任何操作.这是您必须维护和测试的额外事情,而没有任何额外的好处.

And that said, you should ditch the repo entirely. It still requires a dependency on Entity Framework and doesn't do anything but proxy to Entity Framework methods. This is just an extra thing you have to maintain and test with no added benefit.

这篇关于无法为“模型"创建DbSet,因为此类型不包含在上下文的模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆