ASP.NET 5 MVC 6 Generic Repository Pattern

asp.net-core asp.net-core-mvc c# entity-framework-core

Question

Been looking every where for a tutorial or something.

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

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).

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.

Code: my IDbContext interface

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

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.

UPDATE:

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);
}

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

Accepted Answer

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

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

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

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


Popular Answer

On the contrary of firste's answer, I'm not confident by using FirstOrDefault, cause it will generates a SELECT TOP 1 [...].

In many cases, the Id should be unique, but sometimes you can have bad designed Db's. If not, your application should throws an exception (that's what we're looking after).

So, until EF7 implements a Find() method, I strongly suggest using SingleOrDefault() :

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

Like that, you add an application control to verify that Id should be unique, taking no cares if the Db is correctly done or not. It add another level of security to your business.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why