diff --git a/OpenAuth.App/FlowInstanceApp.cs b/OpenAuth.App/FlowInstanceApp.cs index 97e960de..0e18a5f4 100644 --- a/OpenAuth.App/FlowInstanceApp.cs +++ b/OpenAuth.App/FlowInstanceApp.cs @@ -436,7 +436,7 @@ namespace OpenAuth.App public void Update(FlowInstance flowScheme) { - Repository.Update(u => u.Id == flowScheme.Id, u => new FlowInstance()); + Repository.Update(flowScheme); } public TableData Load(QueryFlowInstanceListReq request) diff --git a/OpenAuth.Repository/BaseRepository.cs b/OpenAuth.Repository/BaseRepository.cs index ab0e1f35..e65a493d 100644 --- a/OpenAuth.Repository/BaseRepository.cs +++ b/OpenAuth.Repository/BaseRepository.cs @@ -30,7 +30,7 @@ namespace OpenAuth.Repository } /// - /// 查找单个 + /// 查找单个,且不被上下文所跟踪 /// public T FindSingle(Expression> exp) { @@ -87,9 +87,14 @@ namespace OpenAuth.Repository public void Update(T entity) { var entry = this.Context.Entry(entity); - //todo:如果状态没有任何更改,会报错 entry.State = EntityState.Modified; + //如果数据没有发生变化 + if (!this.Context.ChangeTracker.HasChanges()) + { + return; + } + Save(); } diff --git a/OpenAuth.Repository/OpenAuthDBContext.cs b/OpenAuth.Repository/OpenAuthDBContext.cs index b1e0dad7..ea89daca 100644 --- a/OpenAuth.Repository/OpenAuthDBContext.cs +++ b/OpenAuth.Repository/OpenAuthDBContext.cs @@ -19,9 +19,13 @@ namespace OpenAuth.Repository { Database.SetInitializer< OpenAuthDBContext>(null); } + public OpenAuthDBContext() - :base("Name=OpenAuthDBContext") - { } + : base("Name=OpenAuthDBContext") + { + // 关闭语义可空判断 + Configuration.UseDatabaseNullSemantics = true; + } public OpenAuthDBContext(string nameOrConnectionString) : base(nameOrConnectionString) diff --git a/OpenAuth.Repository/UnitWork.cs b/OpenAuth.Repository/UnitWork.cs index 91a7e88b..1c48bef6 100644 --- a/OpenAuth.Repository/UnitWork.cs +++ b/OpenAuth.Repository/UnitWork.cs @@ -1,151 +1,151 @@ -using System; -using System.Data.Entity; -using System.Data.Entity.Migrations; -using System.Data.Entity.Validation; -using System.Linq; -using System.Linq.Expressions; -using EntityFramework.Extensions; -using Infrastructure; -using OpenAuth.Repository.Interface; - -namespace OpenAuth.Repository -{ - public class UnitWork: IUnitWork - { - protected OpenAuthDBContext Context = new OpenAuthDBContext(); - - - /// - /// 根据过滤条件,获取记录 - /// - /// The exp. - public IQueryable Find(Expression> exp = null) where T : class - { - return Filter(exp); - } - - public bool IsExist(Expression> exp) where T : class - { - return Context.Set().Any(exp); - } - - /// - /// 查找单个 - /// - public T FindSingle(Expression> exp) where T:class - { - return Context.Set().AsNoTracking().FirstOrDefault(exp); - } - - /// - /// 得到分页记录 - /// - /// The pageindex. - /// The pagesize. - /// 排序,格式如:"Id"/"Id descending" - public IQueryable Find(int pageindex, int pagesize, string orderby = "", Expression> exp = null) where T : class - { - if (pageindex < 1) pageindex = 1; - if (string.IsNullOrEmpty(orderby)) - orderby = "Id descending"; - - return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize); - } - - /// - /// 根据过滤条件获取记录数 - /// - public int GetCount(Expression> exp = null) where T : class - { - return Filter(exp).Count(); - } - - public void Add(T entity) where T : Domain.Entity - { - if (string.IsNullOrEmpty(entity.Id)) - { - entity.Id = Guid.NewGuid().ToString(); - } - Context.Set().Add(entity); - } - - /// - /// 批量添加 - /// - /// The entities. - public void BatchAdd(T[] entities) where T : Domain.Entity - { - foreach (var entity in entities) - { - entity.Id = Guid.NewGuid().ToString(); - } - Context.Set().AddRange(entities); - } - - public void Update(T entity) where T:class - { - var entry = this.Context.Entry(entity); - //todo:如果状态没有任何更改,会报错 - entry.State = EntityState.Modified; - - } - - public void Delete(T entity) where T:class - { - Context.Set().Remove(entity); - } - - /// - /// 按指定id更新实体,会更新整个实体 - /// - /// The identity exp. - /// The entity. - public void Update(Expression> identityExp, T entity) where T:class - { - Context.Set().AddOrUpdate(identityExp, entity); - } - - /// - /// 实现按需要只更新部分更新 - /// 如:Update(u =>u.Id==1,u =>new User{Name="ok"}); - /// - /// The where. - /// The entity. - public void Update(Expression> where, Expression> entity) where T:class - { - Context.Set().Where(where).Update(entity); - } - - public virtual void Delete(Expression> exp) where T : class - { - Context.Set().Where(exp).Delete(); - } - - public void Save() - { - try - { - Context.SaveChanges(); - } - catch (DbEntityValidationException e) - { - throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage); - } - - } - - private IQueryable Filter(Expression> exp) where T : class - { - var dbSet = Context.Set().AsQueryable(); - if (exp != null) - dbSet = dbSet.Where(exp); - return dbSet; - } - - public void ExecuteSql(string sql) +using System; +using System.Data.Entity; +using System.Data.Entity.Migrations; +using System.Data.Entity.Validation; +using System.Linq; +using System.Linq.Expressions; +using EntityFramework.Extensions; +using Infrastructure; +using OpenAuth.Repository.Interface; + +namespace OpenAuth.Repository +{ + public class UnitWork: IUnitWork + { + protected OpenAuthDBContext Context = new OpenAuthDBContext(); + + + /// + /// 根据过滤条件,获取记录 + /// + /// The exp. + public IQueryable Find(Expression> exp = null) where T : class + { + return Filter(exp); + } + + public bool IsExist(Expression> exp) where T : class + { + return Context.Set().Any(exp); + } + + /// + /// 查找单个 + /// + public T FindSingle(Expression> exp) where T:class + { + return Context.Set().AsNoTracking().FirstOrDefault(exp); + } + + /// + /// 得到分页记录 + /// + /// The pageindex. + /// The pagesize. + /// 排序,格式如:"Id"/"Id descending" + public IQueryable Find(int pageindex, int pagesize, string orderby = "", Expression> exp = null) where T : class + { + if (pageindex < 1) pageindex = 1; + if (string.IsNullOrEmpty(orderby)) + orderby = "Id descending"; + + return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize); + } + + /// + /// 根据过滤条件获取记录数 + /// + public int GetCount(Expression> exp = null) where T : class + { + return Filter(exp).Count(); + } + + public void Add(T entity) where T : Domain.Entity + { + if (string.IsNullOrEmpty(entity.Id)) + { + entity.Id = Guid.NewGuid().ToString(); + } + Context.Set().Add(entity); + } + + /// + /// 批量添加 + /// + /// The entities. + public void BatchAdd(T[] entities) where T : Domain.Entity + { + foreach (var entity in entities) + { + entity.Id = Guid.NewGuid().ToString(); + } + Context.Set().AddRange(entities); + } + + public void Update(T entity) where T:class + { + var entry = this.Context.Entry(entity); + //todo:如果状态没有任何更改,会报错 + entry.State = EntityState.Modified; + + } + + public void Delete(T entity) where T:class + { + Context.Set().Remove(entity); + } + + /// + /// 按指定id更新实体,会更新整个实体 + /// + /// The identity exp. + /// The entity. + public void Update(Expression> identityExp, T entity) where T:class + { + Context.Set().AddOrUpdate(identityExp, entity); + } + + /// + /// 实现按需要只更新部分更新 + /// 如:Update(u =>u.Id==1,u =>new User{Name="ok"}); + /// + /// The where. + /// The entity. + public void Update(Expression> where, Expression> entity) where T:class + { + Context.Set().Where(where).Update(entity); + } + + public virtual void Delete(Expression> exp) where T : class + { + Context.Set().Where(exp).Delete(); + } + + public void Save() + { + try + { + Context.SaveChanges(); + } + catch (DbEntityValidationException e) + { + throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage); + } + + } + + private IQueryable Filter(Expression> exp) where T : class + { + var dbSet = Context.Set().AsQueryable(); + if (exp != null) + dbSet = dbSet.Where(exp); + return dbSet; + } + + public void ExecuteSql(string sql) { Context.Database.ExecuteSqlCommand(sql); - } - - } -} + } + + } +} diff --git a/OpenAuth.UnitTest/TestWorkflow.cs b/OpenAuth.UnitTest/TestWorkflow.cs index 7504efa5..e3971848 100644 --- a/OpenAuth.UnitTest/TestWorkflow.cs +++ b/OpenAuth.UnitTest/TestWorkflow.cs @@ -49,5 +49,12 @@ namespace OpenAuth.UnitTest Console.WriteLine(JsonHelper.Instance.Serialize(result.Result.FrmPreviewHtml)); } + [TestMethod] + public void TestUpdate() + { //测试数据没有任何修改时EF报错的问题 + var instance = _runApp.Get("d73e4412-9c49-4511-a30e-0d2f844afcee"); + _runApp.Update(instance); + } + } }