Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric Repository - MVC 4 with Linq to Entities
    primarykey
    data
    text
    <p>I think there is something very simple that I am missing or clearly not understanding. I am attempting to create a MVC 4 project that utilizes EF with a model generated by a database. I am attempting to follow the UnitOfWork/Repository design pattern.</p> <p>I have the following interface for a generic repository:</p> <pre><code>public interface IRepository&lt;T&gt; { IEnumerable&lt;T&gt; GetAll(); IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate); T First(Expression&lt;Func&lt;T, bool&gt;&gt; predicate); T FirstOrDefault(Expression&lt;Func&lt;T, bool&gt;&gt; predicate); T GetById(int id); void Add(T entity); void Delete(T entity); } </code></pre> <p>Here is the interface for the UnitOfWork (Note: Topic is a entity from my EF model):</p> <pre><code>public interface IUnitOfWork : IDisposable { IRepository&lt;Topic&gt; TopicRepository { get; } void Commit(); } </code></pre> <p>I am implementing the IRepository as such:</p> <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class { private readonly DbSet&lt;T&gt; _db; public Repository(DbSet&lt;T&gt; db) { this._db = db; } public IQueryable&lt;T&gt; AsQueryable() { throw new NotImplementedException(); } public IEnumerable&lt;T&gt; GetAll() { return this._db; } ... etc } </code></pre> <p>This is the implementation of the UnitOfWork class:</p> <pre><code> public class UnitOfWork : DbContext, IUnitOfWork { private readonly Repository&lt;Topic&gt; _TopicRepository; public DbSet&lt;Topic&gt; Topics { get; set; } public UnitOfWork() { this._TopicRepository = new Repository&lt;Topic&gt;(Topics); } public IRepository&lt;Topic&gt; TopicRepository { get { return this._TopicRepository; } } public void Commit() { this.SaveChanges(); } } </code></pre> <p>I am calling:</p> <pre><code>this._unitOfWork.TopicRepository.GetAll(); </code></pre> <p>which returns zero topics when at least 2 exist in the database. The question I have is I am assuming this makes perfect sense as I no where defined or associated the DbSet to be associated to the CommonGoodEntities(class that was generated by adding the model) that the EF model generated, however how do I associate the two?</p> <p>I hope this makes sense. Essentially I want the UnitOfWork class to use my Entities class.<br> Am I going about this the wrong way?</p> <p>Thanks a lot in advance.</p> <p>Edit:</p> <p>I updated the Unit of work class based on SimonBelanger's comments. After doing so I was able to get it to work with the below code, however, I am not sure it is 100% correct. Is this what I should be doing?</p> <pre><code>public class UnitOfWork : IUnitOfWork { private readonly Repository&lt;Topic&gt; _TopicRepository; private CommonGoodEntities _db; public DbSet&lt;Topic&gt; Topics { get; set; } public UnitOfWork() { this._db = new CommonGoodEntities(); //this._TopicRepository = new Repository&lt;Topic&gt;(Topics); this._TopicRepository = new Repository&lt;Topic&gt;(this._db.Set&lt;Topic&gt;()); } ... etc } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload