ASP.NET MVC 5 6通用存储库模式 [英] ASP.NET 5 MVC 6 Generic Repository Pattern

查看:229
本文介绍了ASP.NET MVC 5 6通用存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在寻找一个教程什么的每一个地方。

Been looking every where for a tutorial or something.

我一直在试图实施MVC5我的老通用的存储库模式进入一个新的项目MVC6

I've been trying to implement my old generic repository pattern for MVC5 into a new MVC6 project.

我设置了3类库的。核心。数据。服务,但是有一个与 IDBset 的问题,看来我的IntelliSense不喜欢它,我试图添加 System.Data这和Entity Framework 6,但没有任何运气(不能找到它......混乱)。

I set up 3 class library's .Core, .Data and .Service, However there's an issue with IDBset, seems my intellisense doesn't like it, I tried to add System.Data and Entity framework 6 but without any luck (cant find it...confusing).

在漫游谷歌我决定问这里有没有教程,正确的方法,或可有人扔了一个非常简单的MVC6通用存储库模式?我有一种感觉做它的老方法可能已经改变,只是不能似乎找到比内置的DI其他任何信息。

After roaming google I decided to ask here, is there a tutorial with the correct way or can someone throw up a very simple MVC6 Generic Repository pattern? I have a feeling the Old method of doing it may have changed, just cant seem to find any information other than the inbuilt DI.

代码:
我的 IDbContext 接口

IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;



不看 IDbSet ,这仅仅是因为的实体框架?我有引用它。

does not see IDbSet, this simply because of Entity Framework? I do have the References to it.

问题可能是我无法找到使用说明书的实体框架。

Issue may be i cant find the using statment for entity framework.

更新:

使用实体框架8.0.0测试版。 。切换到DbSet所有IDbset引用

Using Entity framework 8.0.0 beta. Change all IDbset references to DbSet.

然而,在我的通用库,在这里我使用的方法,如:

However in my generic repository where i use methods such as:

public virtual T GetById(object id)
{
    return this.Entities.Find(id);
}



查找心不是一个方法。我不能再使用DbEntityValidationException我抓。

"Find" isnt a method. and i can no longer use "DbEntityValidationException" in my catchs.

推荐答案

实体框架7 Beta的8不来查找方法。这可能会在最终发布前加入。

Entity Framework 7 Beta 8 doesn't come with the Find Method. It will probably be added before the final release.

您将不得不使用的 FirstOrDefault 方法,而不是直到发生

You will have to use the the FirstOrDefault method instead until that happens

public virtual T GetById(int id)
{
    return this.Entities.FirstOrDefault(x => x.Id == id);
}

由于编号属性将无法识别你必须添加一个接口,使您的存储库实现它。

Because Id property will not be recognized you'll have to add an interface and make your repository implement it.

public interface IEntity
{
     int Id { get; set; }
}



例如

e.g.

 public class GenericRepository<T> : IGenericRepository<T> where T: class, IEntity

从的 GitHub的问题列表。所以DbEntityValidationException不存在EF7 EF7不执行自动数据验证。

From the github issues list. EF7 does not perform automatic data validation so DbEntityValidationException does not exist in EF7.

记:EF7不是EF而是重写的更新

Take note: EF7 is not a update of EF but a rewrite.

这篇关于ASP.NET MVC 5 6通用存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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