ASP.NET 5 MVC 6 Generic Repository Pattern

前端 未结 3 1197
再見小時候
再見小時候 2021-02-10 14:23

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条回答
  •  爱一瞬间的悲伤
    2021-02-10 14:59

    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.

提交回复
热议问题