简体   繁体   English

Visual Studio 2015存储库模式不包含以下定义

[英]Visual Studio 2015 repository pattern does not contain defintion for

I got issues with my repository pattern, it was working this morning so I don't understand here's my code : 我的存储库模式有问题,今天早上它在工作,所以我不明白这是我的代码:

IRepository: iRepository:

public interface IRepository<T> where T : class
{
    List<T> GetAll();
    List<T> GetSome(int index, int pageSize);
    T GetOne(int id);
    T Add(T entity);
    void Update(T entity);
    void Delete(T entity);            
}

IUserRepository : IUserRepository:

interface IUserRepository : IRepository<User>
{
}

UserRepository: UserRepository:

public class UserRepository : IUserRepository
{
    public User Add(User entity)
    {
        throw new NotImplementedException();
    }

    public void Delete(User entity)
    {
        throw new NotImplementedException();
    }

    public List<User> GetAll()
    {
        return new SchoolContext().User.ToList();
    }

    public User GetOne(int id)
    {
        throw new NotImplementedException();
    }

    public List<User> GetSome(int index, int pageSize)
    {
        throw new NotImplementedException();
    }

    public void Update(User entity)
    {
        throw new NotImplementedException();
    }
}

Test File : 测试文件:

class Program
{
    static void Main(string[] args)
    {
        var source = new User().GetAll();
        foreach (var item in source)
        {
            Console.WriteLine(item.Login);
        }

        Console.Read();

    }
}

The error I get in the test file is : 我在测试文件中得到的错误是:

User does not contain a definition for 'GetAll' and no extension method'GetAll' accepting a first argument of type 'User' could be found 用户不包含“ GetAll”的定义,找不到扩展方法“ GetAll”,该扩展方法接受“ User”类型的第一个参数

I just want to display the list of the login in the console, what did I do wrong ? 我只想在控制台中显示登录列表,我做错了什么?

You should create repository: 您应该创建存储库:

var source = new UserRepository().GetAll();
                       ^

But you are creating User entity instead. 但是,您正在创建User实体。

HINT: Instead of creating context each time you call any method on repository, you should pass context to repository and use one context for all operations. 提示:而不是每次在存储库上调用任何方法时都创建上下文,而应将上下文传递到存储库,并对所有操作使用一个上下文。 Otherwise you will have to attach entities to new context for modification, because entities will not be tracked by new context. 否则,您将必须将实体附加到新的上下文中进行修改,因为新上下文不会跟踪实体。 And it's better to control lifetime of context to avoid context disposed kind of errors when working with lazy-loaded entities. 并且最好控制上下文的生存期,以避免在处理延迟加载的实体时上下文处理的错误。

public class UserRepository : IUserRepository
{
    private SchoolContext db;

    public UserRepository(SchoolContext db)
    {
        this.db = db;
    }

    public List<User> GetAll()
    {
        return db.User.ToList();
    }    
}

Event more - you can create base abstract repository Repository<T> which will provide such general functionality via method of Set<T> from context. 如果有更多活动,您可以创建基本的抽象存储库Repository<T> ,它将通过上下文中的Set<T>方法提供此类常规功能。

You've simply made a typo. 您只是打错了字。 Instantiate a class of type UserRepository where you are instantiating a class of type User and you'll be all set. 实例化UserRepository类型的类,在此实例化User类型的类,您将全部准备就绪。

var repository = new UserRepository();
var users = repository.GetAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IServiceCollection不包含AddHttpClient的定义 - IServiceCollection does not contain a defintion for AddHttpClient IGrouping <decimal,string> 不包含“名称”的定义 - IGrouping <decimal,string> does not contain a defintion for 'name' Visual Studio 2015中的项目是否包含完全编译为一个可执行文件或多个可执行文件的代码? - Does a project in Visual Studio 2015 contain code compiled into exactly one executable or multiple ones? “KeyPressEventArgs”不包含 KeyCode 错误的定义 - 'KeyPressEventArgs' does not contain defintion for KeyCode error 为什么此代码会使Visual Studio 2015崩溃? - Why does this code crash Visual Studio 2015? Visual Studio 2015 - Xamarin - Android - 当我尝试在 .cs 文件中执行任何操作时,获取“resource.id 不包含 xxx 的定义” - Visual Studio 2015 - Xamarin - Android - Getting “resource.id does not contain a definition for xxx” when I try to do anything in the .cs file System.Web.WebPages.Html.HtmlHelper不包含定义 - System.Web.WebPages.Html.HtmlHelper does not contain a defintion Visual Studio 2012错误“不包含定义” - Visual Studio 2012 Error “Does not contain a definition” 使用N层存储库模式Visual Studio项目启用迁移 - Enable migrations with N-Tier Repository pattern Visual studio project 使用Visual Studio 2015实现无状态 - Stateless with Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM