Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET 5 MVC 6通用存储库模式_C#_Asp.net Core_Asp.net Core Mvc_Entity Framework Core - Fatal编程技术网

C# ASP.NET 5 MVC 6通用存储库模式

C# ASP.NET 5 MVC 6通用存储库模式,c#,asp.net-core,asp.net-core-mvc,entity-framework-core,C#,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,到处找教程什么的 我一直在尝试将旧的MVC5通用存储库模式实现到新的MVC6项目中 我设置了3个类库的.Core、.Data和.Service,但是IDBset有一个问题,似乎我的intellisense不喜欢它,我尝试添加系统、数据和实体框架6,但没有任何运气(找不到它…令人困惑) 在漫游谷歌之后,我决定在这里问,是否有一个正确方法的教程,或者有人能提供一个非常简单的MVC6通用存储库模式?我有一种感觉,以前做这件事的方法可能已经改变了,只是除了内置的DI之外,似乎找不到任何信息 代码: 我的

到处找教程什么的

我一直在尝试将旧的MVC5通用存储库模式实现到新的MVC6项目中

我设置了3个类库的
.Core
.Data
.Service
,但是
IDBset
有一个问题,似乎我的intellisense不喜欢它,我尝试添加
系统、数据和实体框架6,但没有任何运气(找不到它…令人困惑)

在漫游谷歌之后,我决定在这里问,是否有一个正确方法的教程,或者有人能提供一个非常简单的MVC6通用存储库模式?我有一种感觉,以前做这件事的方法可能已经改变了,只是除了内置的DI之外,似乎找不到任何信息

代码: 我的
IDbContext
界面

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

“查找”不是一种方法。而且我不能再在我的catch中使用“DbEntityValidationException”。

实体框架7 Beta 8没有Find方法。它可能会在最终版本之前添加

在此之前,您必须使用
FirstOrDefault
方法

public virtual T GetById(int id)
{
    return this.Entities.FirstOrDefault(x => x.Id == id);
}
因为
Id
属性将无法识别,所以您必须添加一个接口,并让存储库实现它

public interface IEntity
{
     int Id { get; set; }
}
e、 g

公共类GenericRepository:IGenericRepository,其中T:class,Entity
从。EF7不执行自动数据验证,因此EF7中不存在DbEntityValidationException


注意:EF7不是EF的更新,而是一种重写。

在EntityFramework 7
IDbSet
中,EF7本身表示存储库模式的实现

/// <summary>
///     A DbContext instance represents a session with the database and can be used to query and save
///     instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.
/// </summary>
//
///DbContext实例表示与数据库的会话,可用于查询和保存
///实体的实例。DbContext是工作单元和存储库模式的组合。
/// 
来源:GitHub EntityFramework7 repo


DBContext它的实现

与firste的回答相反,我不相信使用FirstOrDefault,因为它将生成一个选择的前1[…]

在许多情况下,Id应该是唯一的,但有时可能会有设计不好的Db。如果没有,应用程序应该抛出一个异常(这就是我们正在关注的)

因此,在EF7实现Find()方法之前,我强烈建议使用SingleOrDefault()

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

这样,您就可以添加一个应用程序控件来验证Id是否应该是唯一的,而不管Db是否正确完成。它为您的业务增加了另一个级别的安全性。

我认为展示一些代码会有所帮助。我的意思是,“IDBSet”位于
EntityFramework.dll
中。当然,我会获取一些我的代码。您是否有意引用EntityFramework 6.1.3?ASP.NET5随附,但这是完全重写。7不稳定,是吗?事实上,嗯,我应该使用7作为它的内置asp.NET5,“使用实体框架8.0.0测试版”。。。没有这样的事。你是EF7吗,beta8?在这种情况下需要转换,x=>x.Id==(int)Id“DbEntityValidationException”怎么办?这似乎不再存在于实体中了。还有其他选择吗?啊,很遗憾,已经放弃了对不起,你是想说我不需要回购模式,因为实体已经实现了一种模式吗?@SteveSmithSkId yep,微软的开发人员对EntityFramework做了一些架构上的更改,并以MVC框架中通常的方式实现了上下文内容。现在它把我看作是多余的代码。。要在ASP.NET5-MVC6中实现UoW和存储库。。。他们还增加了为任何类型的数据库创建自己的数据上下文的架构可能性。。无论是关系型还是NoSQL,都不再有IDbSet(EF核心)。只是想知道他们为什么把它拿走。
/// <summary>
///     A DbContext instance represents a session with the database and can be used to query and save
///     instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.
/// </summary>
public virtual T GetById(int id)
{
    return this.Entities.SingleOrDefault(x => x.Id == id);
}