开发者

Entity Framework Generic Repository

开发者 https://www.devze.com 2023-03-26 17:57 出处:网络
I am writing a generic repository to be used for my every model CRUD operation using entity framework CTP5 as following:

I am writing a generic repository to be used for my every model CRUD operation using entity framework CTP5 as following:

  public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
    {
        public DbContext Context { get; set; }


        public void Insert(TEntity entity)
        {
                if (Context.Entry<TEntity>(entity).State == EntityState.Detached)
                {
                    Context.Set<TEntity>().Attach(entity);
                }
                Context.Set<TEntity>().Add(entity);
                Context.SaveChanges();

        }
        public void Delete(int id)
        {
                TEntity entity = Context.Set<TEntity>().Find(id);
                if (Context.Entry<TEntity>(entity).State == EntityState.Detached)
                {
                    Context.Set<TEntity>().Attach(entity);
                }
                Context.Set<TEntity>().Remove(entity);
                Context.SaveChanges();

        }
        public void Delete(TEntity entity)
        {
                Context.Set<TEntity>().Remove(entity);
                Context.SaveChanges();

        }
        public void Update(TEntity entity)
        {
                TEntity status = Context.Set<TEntity>().Find(entity.Id);
                status = entity;
                Context.SaveChanges();

        }



        public TEntity GetFirst()
        {

                var entity = Context.Set<TEntity>().FirstOrDefault();
                if (entity == null) return null;
                return entity;


        }
        public TEntity GetNext(int id)
        {

                var entity = (from u in Context.Set<TEntity>()
                              where u.Id > id
                              select u).FirstOrDefault();
                if (entity == null) return null;
                return entity;

        }
        public TEntity GetPrevoius(int id)
        {
开发者_如何学JAVA
                var entity = (from u in Context.Set<TEntity>()
                                where u.Id < id
                                orderby u.Id descending
                                select u).FirstOrDefault();
                if (entity == null) return GetFirst();
                return entity;
        }
        public TEntity GetLast()
        {

                var entity = (Context.Set<TEntity>().OrderByDescending(u => u.Id)).FirstOrDefault();
                if (entity == null) return null;
                return entity;

        }
        public TEntity GetById(int id)
        {
            return Context.Set<TEntity>().Find(id);

        }
        public int GetMaxId()
        {

                var max = Context.Set<TEntity>().Count()+ 1;
                return max;

        }
}

everything works fine but Update method which nither doesnt generate any error nor save any changes back to database. Can anybody guid me how to solve this issue?


You can use CurrentValues.SetValues:

public void Update(TEntity entity)
{
    TEntity status = Context.Set<TEntity>().Find(entity.Id);
    Context.Entry(status).CurrentValues.SetValues(entity);
    Context.SaveChanges();
}

It updates scalar and complex properties but not navigation properties.


You're overwriting the variable status with a totally new object, taking the one from the database out of scope, but not actually modifying the object that is attached to the context, which is what you'll want to do.

The only way I can think off the top of my head is to use reflection to read all the properties of the type, and assign the values to the original object based on the new one, something like:

foreach (var prop in typeof(TEntity).GetProperties())
{
    prop.SetValue(status, prop.GetValue(entity, null), null);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消