check bugs

This commit is contained in:
yubaolee@163.com 2018-05-07 14:47:16 +08:00
parent 37a371a138
commit ebf037f518
5 changed files with 170 additions and 154 deletions

View File

@ -436,7 +436,7 @@ namespace OpenAuth.App
public void Update(FlowInstance flowScheme) public void Update(FlowInstance flowScheme)
{ {
Repository.Update(u => u.Id == flowScheme.Id, u => new FlowInstance()); Repository.Update(flowScheme);
} }
public TableData Load(QueryFlowInstanceListReq request) public TableData Load(QueryFlowInstanceListReq request)

View File

@ -30,7 +30,7 @@ namespace OpenAuth.Repository
} }
/// <summary> /// <summary>
/// 查找单个 /// 查找单个,且不被上下文所跟踪
/// </summary> /// </summary>
public T FindSingle(Expression<Func<T, bool>> exp) public T FindSingle(Expression<Func<T, bool>> exp)
{ {
@ -87,9 +87,14 @@ namespace OpenAuth.Repository
public void Update(T entity) public void Update(T entity)
{ {
var entry = this.Context.Entry(entity); var entry = this.Context.Entry(entity);
//todo:如果状态没有任何更改,会报错
entry.State = EntityState.Modified; entry.State = EntityState.Modified;
//如果数据没有发生变化
if (!this.Context.ChangeTracker.HasChanges())
{
return;
}
Save(); Save();
} }

View File

@ -19,9 +19,13 @@ namespace OpenAuth.Repository
{ {
Database.SetInitializer< OpenAuthDBContext>(null); Database.SetInitializer< OpenAuthDBContext>(null);
} }
public OpenAuthDBContext() public OpenAuthDBContext()
:base("Name=OpenAuthDBContext") : base("Name=OpenAuthDBContext")
{ } {
// 关闭语义可空判断
Configuration.UseDatabaseNullSemantics = true;
}
public OpenAuthDBContext(string nameOrConnectionString) public OpenAuthDBContext(string nameOrConnectionString)
: base(nameOrConnectionString) : base(nameOrConnectionString)

View File

@ -1,151 +1,151 @@
using System; using System;
using System.Data.Entity; using System.Data.Entity;
using System.Data.Entity.Migrations; using System.Data.Entity.Migrations;
using System.Data.Entity.Validation; using System.Data.Entity.Validation;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using EntityFramework.Extensions; using EntityFramework.Extensions;
using Infrastructure; using Infrastructure;
using OpenAuth.Repository.Interface; using OpenAuth.Repository.Interface;
namespace OpenAuth.Repository namespace OpenAuth.Repository
{ {
public class UnitWork: IUnitWork public class UnitWork: IUnitWork
{ {
protected OpenAuthDBContext Context = new OpenAuthDBContext(); protected OpenAuthDBContext Context = new OpenAuthDBContext();
/// <summary> /// <summary>
/// 根据过滤条件,获取记录 /// 根据过滤条件,获取记录
/// </summary> /// </summary>
/// <param name="exp">The exp.</param> /// <param name="exp">The exp.</param>
public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class
{ {
return Filter(exp); return Filter(exp);
} }
public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class
{ {
return Context.Set<T>().Any(exp); return Context.Set<T>().Any(exp);
} }
/// <summary> /// <summary>
/// 查找单个 /// 查找单个
/// </summary> /// </summary>
public T FindSingle<T>(Expression<Func<T, bool>> exp) where T:class public T FindSingle<T>(Expression<Func<T, bool>> exp) where T:class
{ {
return Context.Set<T>().AsNoTracking().FirstOrDefault(exp); return Context.Set<T>().AsNoTracking().FirstOrDefault(exp);
} }
/// <summary> /// <summary>
/// 得到分页记录 /// 得到分页记录
/// </summary> /// </summary>
/// <param name="pageindex">The pageindex.</param> /// <param name="pageindex">The pageindex.</param>
/// <param name="pagesize">The pagesize.</param> /// <param name="pagesize">The pagesize.</param>
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param> /// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class
{ {
if (pageindex < 1) pageindex = 1; if (pageindex < 1) pageindex = 1;
if (string.IsNullOrEmpty(orderby)) if (string.IsNullOrEmpty(orderby))
orderby = "Id descending"; orderby = "Id descending";
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize); return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
} }
/// <summary> /// <summary>
/// 根据过滤条件获取记录数 /// 根据过滤条件获取记录数
/// </summary> /// </summary>
public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class
{ {
return Filter(exp).Count(); return Filter(exp).Count();
} }
public void Add<T>(T entity) where T : Domain.Entity public void Add<T>(T entity) where T : Domain.Entity
{ {
if (string.IsNullOrEmpty(entity.Id)) if (string.IsNullOrEmpty(entity.Id))
{ {
entity.Id = Guid.NewGuid().ToString(); entity.Id = Guid.NewGuid().ToString();
} }
Context.Set<T>().Add(entity); Context.Set<T>().Add(entity);
} }
/// <summary> /// <summary>
/// 批量添加 /// 批量添加
/// </summary> /// </summary>
/// <param name="entities">The entities.</param> /// <param name="entities">The entities.</param>
public void BatchAdd<T>(T[] entities) where T : Domain.Entity public void BatchAdd<T>(T[] entities) where T : Domain.Entity
{ {
foreach (var entity in entities) foreach (var entity in entities)
{ {
entity.Id = Guid.NewGuid().ToString(); entity.Id = Guid.NewGuid().ToString();
} }
Context.Set<T>().AddRange(entities); Context.Set<T>().AddRange(entities);
} }
public void Update<T>(T entity) where T:class public void Update<T>(T entity) where T:class
{ {
var entry = this.Context.Entry(entity); var entry = this.Context.Entry(entity);
//todo:如果状态没有任何更改,会报错 //todo:如果状态没有任何更改,会报错
entry.State = EntityState.Modified; entry.State = EntityState.Modified;
} }
public void Delete<T>(T entity) where T:class public void Delete<T>(T entity) where T:class
{ {
Context.Set<T>().Remove(entity); Context.Set<T>().Remove(entity);
} }
/// <summary> /// <summary>
/// 按指定id更新实体,会更新整个实体 /// 按指定id更新实体,会更新整个实体
/// </summary> /// </summary>
/// <param name="identityExp">The identity exp.</param> /// <param name="identityExp">The identity exp.</param>
/// <param name="entity">The entity.</param> /// <param name="entity">The entity.</param>
public void Update<T>(Expression<Func<T, object>> identityExp, T entity) where T:class public void Update<T>(Expression<Func<T, object>> identityExp, T entity) where T:class
{ {
Context.Set<T>().AddOrUpdate(identityExp, entity); Context.Set<T>().AddOrUpdate(identityExp, entity);
} }
/// <summary> /// <summary>
/// 实现按需要只更新部分更新 /// 实现按需要只更新部分更新
/// <para>如Update(u =>u.Id==1,u =>new User{Name="ok"});</para> /// <para>如Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
/// </summary> /// </summary>
/// <param name="where">The where.</param> /// <param name="where">The where.</param>
/// <param name="entity">The entity.</param> /// <param name="entity">The entity.</param>
public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class
{ {
Context.Set<T>().Where(where).Update(entity); Context.Set<T>().Where(where).Update(entity);
} }
public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class
{ {
Context.Set<T>().Where(exp).Delete(); Context.Set<T>().Where(exp).Delete();
} }
public void Save() public void Save()
{ {
try try
{ {
Context.SaveChanges(); Context.SaveChanges();
} }
catch (DbEntityValidationException e) catch (DbEntityValidationException e)
{ {
throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage); throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
} }
} }
private IQueryable<T> Filter<T>(Expression<Func<T, bool>> exp) where T : class private IQueryable<T> Filter<T>(Expression<Func<T, bool>> exp) where T : class
{ {
var dbSet = Context.Set<T>().AsQueryable(); var dbSet = Context.Set<T>().AsQueryable();
if (exp != null) if (exp != null)
dbSet = dbSet.Where(exp); dbSet = dbSet.Where(exp);
return dbSet; return dbSet;
} }
public void ExecuteSql(string sql) public void ExecuteSql(string sql)
{ {
Context.Database.ExecuteSqlCommand(sql); Context.Database.ExecuteSqlCommand(sql);
} }
} }
} }

View File

@ -49,5 +49,12 @@ namespace OpenAuth.UnitTest
Console.WriteLine(JsonHelper.Instance.Serialize(result.Result.FrmPreviewHtml)); 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);
}
} }
} }