C# 通用存储库,CreateObjectSet<;T>;()方法

C# 通用存储库,CreateObjectSet<;T>;()方法,c#,asp.net,entity-framework,asp.net-web-api,repository-pattern,C#,Asp.net,Entity Framework,Asp.net Web Api,Repository Pattern,我试图实现一个通用存储库,现在我有了这个: using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Data.Entity.Core.Objects; using Web_API.Models; namespace Web_API.DAL { class GenericRepository<T> : IRepository<T&

我试图实现一个通用存储库,现在我有了这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity.Core.Objects;
using Web_API.Models;

namespace Web_API.DAL
{
    class GenericRepository<T> : IRepository<T> where T : class
    {
        private ApplicationDbContext entities = null;
        IObjectSet<T> _objectSet;

        public GenericRepository(ApplicationDbContext _entities)
        {
            entities = _entities;
            _objectSet = entities.CreateObjectSet<T>();
        }

        ...
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统数据;
使用System.Data.Entity.Core.Objects;
使用Web_API.Models;
名称空间Web_API.DAL
{
类GenericRepository:IRepository,其中T:class
{
私有ApplicationDbContext实体=null;
IObjectSet\u objectSet;
公共通用存储库(ApplicationDbContext\u实体)
{
实体=_实体;
_objectSet=entities.CreateObjectSet();
}
...
我在使用此方法调用时遇到问题:
entities.CreateObjectSet();
应该没问题,但是我得到了这个错误:


我已经将System.Data.Entity添加到我的项目中,现在我不知道还能做什么。我正在学习本教程。有人知道如何解决此问题吗?

您需要更改方法,使其看起来像这样:

public GenericRepository(ApplicationDbContext _entities)
{
    entities = _entities;
    _objectSet = entities.Set<T>(); //This line changed.
}

非常感谢!CreateObjectSet()在最新版本的EF中是否已被弃用?
。CreateObjectSet()
ObjectContext
的一种方法,据我所知,该方法在EF 4.1中已被
DbContext
替换。如果这回答了您的问题,请确保将其标记为该方法。我已更改为entities.Set()但这仍然给了我一个错误:非常感谢你的帮助。
DbSet<T> _objectSet;