ASP.NET 5 MVC 6 Generic Repository Pattern

前端 未结 3 2068
清酒与你
清酒与你 2021-02-10 14:34

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:45

    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 : IGenericRepository 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.

提交回复
热议问题