使用实体框架

时间:2015-06-12 09:22:31

标签: entity-framework unit-of-work

我的要求是我需要从表中删除一些行,然后在同一个表中插入一些行。我正在使用工作单元,因此删除和插入都是同一事务的一部分。但是当我试图保存数据时,实体框架是重复键抛出错误。请在下面找到示例和代码:

示例:表名 - 表1,列 - col1(c.K),col2(C.K),col3

行删除 - 78,1,1

RowTo Add- 78,1,1            78,2,2

我的工作单位:

public class DataRepository<T> : IRepository<T> where T:class // IDisposable,
    {
        #region Variables

        private readonly CWSEntities _context;
        protected readonly IDbSet<T> _dbset;

        #endregion

        #region Constructors

        public DataRepository() 
       {
           _context = new CWSEntities();
           _dbset = _context.Set<T>();

       }
        public DataRepository(CWSEntities context) 
       {
           _context =context;
           _dbset = _context.Set<T>();

       }

        #endregion

        #region Methods

        public IQueryable<T> All() 
        {
            return _context.Set<T>();
        }

      //  public IQueryable<T> AllInclude(params Expression<Func<T,object>>[] include) 
        public IQueryable<T> Include(params Expression<Func<T, object>>[] include)
        {
            IQueryable<T> retValue = _context.Set<T>();

            foreach (var item in include)
            {
                retValue = retValue.Include(item);
            }
            return retValue;
        }

        public T GetById(object id)
        {
            return this._dbset.Find(id);
        }      

        public  IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
           string includeProperties = "")
        {
            IQueryable<T> 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  T Add(T entity)  
        {
            if (entity != null)
            {
                return _dbset.Add(entity);
            }
            return null;
        }

        public  void Delete(object id)
        {
            T entityToDelete = _dbset.Find(id);
                        
            Delete(id);
        }

        public  void Delete(T entityToDelete)
        {
            if (entityToDelete != null)
            {
                if (_context.Entry(entityToDelete).State == EntityState.Detached)
                {
                    _dbset.Attach(entityToDelete);
                }
                _dbset.Remove(entityToDelete);
            }
           
        }

        public  void Update(T entityToUpdate)
        {
            if (entityToUpdate != null)
            {
                _dbset.Attach(entityToUpdate);
                _context.Entry(entityToUpdate).State = EntityState.Modified;
                // _context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            }
        }

        public virtual void Save()
        {
            try
            {
                _context.SaveChanges();
            }
            catch(DbEntityValidationException exception)
            {

            }
        }

1 个答案:

答案 0 :(得分:1)

从我可以收集的内容中,您尝试添加具有相同主键的实体,如您在示例中所述

  

RowTo Add- 78,1,1 78,2,2

看起来您的Add方法无法正确处理此问题。您可以通过传递实体的主键值来检查实体是否存在,如果不存在,则执行添加,如果不是则可能更新?

 public  T Add(T entity, params object[] keys)  
 {
    if (entity != null)
    {
        var existing = _dbset.Find(keys)
        if (existing == null)
            return _dbset.Add(entity);
        else
            Update(entity);
    }
    return null;
}