实体框架通用存储库 [英] Entity Framework Generic Repository

查看:97
本文介绍了实体框架通用存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通用资源库,用于使用实体框架CTP5的每个模型CRUD操作,如下所示:

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

$ b 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();

$ b 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();

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

$ b 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;
返回实体;


$ b public TEntity GetNext(int id)
{

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

$ b public TEntity GetPrevoius(int id)
{

var entity =(from Context.Set< TEntity>()
其中u.Id< id
orderby u.Id降序
选择你).FirstOrDefault();
if(entity == null)return GetFirst();
返回实体;
}
public TEntity GetLast()
{

var entity =(Context.Set< TEntity>()。OrderByDescending(u => u.Id) ).FirstOrDefault();
if(entity == null)return null;
返回实体;

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

$ b public int GetMaxId()
{

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


$ b $ / code $ / pre

一切正常,但Update方法不会产生任何错误,也不会将任何更改保存回数据库。
任何人都可以指导我如何解决这个问题? 您正在覆盖变量这是一个全新的对象,将数据库中的一个从数据库中抽取出来,但实际上并没有修改连接到上下文的对象,我希望能做到这一点。



我可以从头开始思考的唯一方法是使用反射读取类型的所有属性,并将值分配给基于新对象的原始对象,如下所示:

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


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)
        {

                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'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);
}

这篇关于实体框架通用存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆