From 9faedc12601859ce5f1e422e8fe5d7e454a84796 Mon Sep 17 00:00:00 2001 From: yubao Date: Sun, 20 May 2018 13:54:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=BA=E6=9E=84=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BC=96=E8=BE=91=E7=9A=84=E5=BC=82=E5=B8=B8=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/OrgManagerApp.cs | 2 ++ OpenAuth.Mvc/Controllers/OrgManagerController.cs | 9 +-------- OpenAuth.Mvc/Controllers/UserSessionController.cs | 8 +++++++- OpenAuth.Repository/BaseRepository.cs | 2 +- OpenAuth.Repository/OpenAuthDBContext.cs | 6 ++++++ OpenAuth.Repository/UnitWork.cs | 9 +++++++-- OpenAuth.UnitTest/TestOrg.cs | 14 +++++++++++++- 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/OpenAuth.App/OrgManagerApp.cs b/OpenAuth.App/OrgManagerApp.cs index 337cb082..7b5562af 100644 --- a/OpenAuth.App/OrgManagerApp.cs +++ b/OpenAuth.App/OrgManagerApp.cs @@ -43,6 +43,8 @@ namespace OpenAuth.App UnitWork.Update(a); } + UnitWork.Save(); + return org.Id; } diff --git a/OpenAuth.Mvc/Controllers/OrgManagerController.cs b/OpenAuth.Mvc/Controllers/OrgManagerController.cs index 14066c4f..cd69f213 100644 --- a/OpenAuth.Mvc/Controllers/OrgManagerController.cs +++ b/OpenAuth.Mvc/Controllers/OrgManagerController.cs @@ -18,13 +18,7 @@ namespace OpenAuth.Mvc.Controllers { return View(); } - public ActionResult Assign(string firstId, string key) - { - ViewBag.FirstId = firstId; - ViewBag.ModuleType = key; - return View(); - } - + public string LoadForUser(string firstId) { var orgs = OrgApp.LoadForUser(firstId); @@ -72,7 +66,6 @@ namespace OpenAuth.Mvc.Controllers /// /// 删除指定ID的组织 - /// Id为逗号分开的字符串 /// /// System.String. [HttpPost] diff --git a/OpenAuth.Mvc/Controllers/UserSessionController.cs b/OpenAuth.Mvc/Controllers/UserSessionController.cs index 1c1a2e72..c0b71eee 100644 --- a/OpenAuth.Mvc/Controllers/UserSessionController.cs +++ b/OpenAuth.Mvc/Controllers/UserSessionController.cs @@ -78,7 +78,13 @@ namespace OpenAuth.Mvc.Controllers { var org = user.Orgs.SingleOrDefault(u => u.Id == orgId); if (org == null) - throw new Exception("未能找到指定对象信息"); + { + return JsonHelper.Instance.Serialize(new TableData + { + msg ="未找到指定的节点", + code = 500, + }); + } cascadeId = org.CascadeId; } diff --git a/OpenAuth.Repository/BaseRepository.cs b/OpenAuth.Repository/BaseRepository.cs index e65a493d..ba2ee536 100644 --- a/OpenAuth.Repository/BaseRepository.cs +++ b/OpenAuth.Repository/BaseRepository.cs @@ -145,7 +145,7 @@ namespace OpenAuth.Repository private IQueryable Filter(Expression> exp) { - var dbSet = Context.Set().AsQueryable(); + var dbSet = Context.Set().AsNoTracking().AsQueryable(); if (exp != null) dbSet = dbSet.Where(exp); return dbSet; diff --git a/OpenAuth.Repository/OpenAuthDBContext.cs b/OpenAuth.Repository/OpenAuthDBContext.cs index ea89daca..b8966a61 100644 --- a/OpenAuth.Repository/OpenAuthDBContext.cs +++ b/OpenAuth.Repository/OpenAuthDBContext.cs @@ -8,6 +8,7 @@ //------------------------------------------------------------------------------ using System.Data.Entity; +using System.Data.Entity.Infrastructure; using OpenAuth.Repository.Domain; using OpenAuth.Repository.Mapping; @@ -25,6 +26,11 @@ namespace OpenAuth.Repository { // 关闭语义可空判断 Configuration.UseDatabaseNullSemantics = true; + + //与变量的值为null比较 + //ef判断为null的时候,不能用变量比较:https://stackoverflow.com/questions/682429/how-can-i-query-for-null-values-in-entity-framework?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa + (this as IObjectContextAdapter).ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior = true; + Database.Log = s => System.Diagnostics.Debug.WriteLine(s); } public OpenAuthDBContext(string nameOrConnectionString) diff --git a/OpenAuth.Repository/UnitWork.cs b/OpenAuth.Repository/UnitWork.cs index 1c48bef6..581841df 100644 --- a/OpenAuth.Repository/UnitWork.cs +++ b/OpenAuth.Repository/UnitWork.cs @@ -85,9 +85,14 @@ namespace OpenAuth.Repository public void Update(T entity) where T:class { var entry = this.Context.Entry(entity); - //todo:如果状态没有任何更改,会报错 entry.State = EntityState.Modified; + //如果数据没有发生变化 + if (!this.Context.ChangeTracker.HasChanges()) + { + entry.State = EntityState.Unchanged; + } + } public void Delete(T entity) where T:class @@ -136,7 +141,7 @@ namespace OpenAuth.Repository private IQueryable Filter(Expression> exp) where T : class { - var dbSet = Context.Set().AsQueryable(); + var dbSet = Context.Set().AsNoTracking().AsQueryable(); if (exp != null) dbSet = dbSet.Where(exp); return dbSet; diff --git a/OpenAuth.UnitTest/TestOrg.cs b/OpenAuth.UnitTest/TestOrg.cs index e1f6e7f0..d9ac8cf8 100644 --- a/OpenAuth.UnitTest/TestOrg.cs +++ b/OpenAuth.UnitTest/TestOrg.cs @@ -23,7 +23,7 @@ namespace OpenAuth.UnitTest { try { - for (int i = 0; i < 2; i++) + for (int i = 0; i < 5; i++) { var random = new Random(); int val = random.Next(); @@ -43,5 +43,17 @@ namespace OpenAuth.UnitTest Console.WriteLine(e.Message); } } + + + [TestMethod] + public void update() + { + _app.Update(new Org + { + Id = "4bcdd85d-662f-42e4-94dc-19e3e98ef4dc", + Name = "测试", + ParentId = "57239263-0176-4ef9-a4b5-3491a04e2c6a" + }); + } } }