Does anyone have a very complete example generic repository for EF 6.1?

后端 未结 3 1377
囚心锁ツ
囚心锁ツ 2020-12-25 08:21

I have my own repository that is as follows. However this does not take into account some of the new features such as the range features. Does anyone have a repository that

3条回答
  •  情歌与酒
    2020-12-25 08:47

    Take a look at Generic Unit of Work & Repositories Framework. You can download the entire repository project, modify the code to include the range functions, implement it in your own solution, etc.

    Here's an example of its usage in the context of an OData/WebAPI controller method returning a DTO, rather than the EF entity.

                var results = odataQueryOptions.ApplyTo(_uow.Repository()
                    .Query()
                    .Get()
                    .Include(u => u.User)
                    .Where(u => u.UserId == userId)
                    .OrderBy(o => o.Description)).Cast()
                    .Select(x => new ContentTypeDTO()
                    {
                        //projection goes here
                        ContentTypeId = x.ContentTypeId,
                        Description = x.Description,
                        UserDTO = new UserDTO 
                        { 
                            UserId = x.UserId,
                            UserName = x.User.UserName
                        }
                    });
    

    Hope that helps.

提交回复
热议问题