문제

I'm very new to MVC 3 but not to software so when I decided to start an application in MVC 3, I wanted to make it testable. So I found this great article on how to implement the repository pattern.

In this article, at the end, they propose a very nice "generic repository" that you can re-use for most object because in normal case, we all want to be able to delete, create, update, etc.

Everything was working fine (read compiling) until I realized that I was pointing to my old database context that was created a while ago with the "Code First" approach. Since that time, I decided to go with a "Database First" approach but I forgot to delete this old context. So I just change the name of the context in the repository hoping that everything would work fine but it doesn't work. My old context was an DBContext but my new one seems to be an ObjectContext. I'm not sure what is the difference between the 2.

The code in my generic repository goes as follow and there is error about these 3 lines about the Set and Entry Method:

this.dbSet = context.Set<TEntity>();

if (context.Entry(entityToDelete).State == EntityState.Detached)

context.Entry(entityToUpdate).State = EntityState.Modified;

Here is the complete code for the repository

public class GenericRepository<TEntity> where TEntity : class
{
    internal RecettesMaisonDBEntities context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(RecettesMaisonDBEntities context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = dbSet.Find(id);
        Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (context.Entry(entityToDelete).State == EntityState.Detached)
        {
            dbSet.Attach(entityToDelete);
        }
        dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        context.Entry(entityToUpdate).State = EntityState.Modified;
    }
}

If anyone can help me with this?

도움이 되었습니까?

해결책

Sounds like you're not using the DbContext code generator.

http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx

Note, this is slightly out of date because they renamed the generators to separate the 4.x and 5.x versions. Use whichever one is appropriate for your situation.

Then, since your context is DbContext derived, you can use it in your repository.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top