CodeFirst DbFirst Cache BUG

This commit is contained in:
sunkaixuan 2017-07-03 01:47:13 +08:00
parent 9f34948ed1
commit bdf758c16f
5 changed files with 27 additions and 8 deletions

View File

@ -22,6 +22,7 @@ namespace SqlSugar
} }
public virtual void InitTables(Type entityType) public virtual void InitTables(Type entityType)
{ {
this.Context.RewritableMethods.RemoveCacheAll();
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
{ {
Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); Check.Exception(true, "Dbfirst and Codefirst requires system table permissions");

View File

@ -39,6 +39,7 @@ namespace SqlSugar
public void Init() public void Init()
{ {
this.Context.RewritableMethods.RemoveCacheAll();
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
{ {
Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); Check.Exception(true, "Dbfirst and Codefirst requires system table permissions");

View File

@ -41,7 +41,8 @@ namespace SqlSugar
if (_instance == null) if (_instance == null)
{ {
_instance = new CacheManager<V>(); _instance = new CacheManager<V>();
CacheManager.Add(_instance.InstanceCache); Action addItem =()=> { CacheManager<V>.GetInstance().RemoveAllCache(); };
CacheManager.Add(addItem);
} }
return _instance; return _instance;
} }
@ -62,6 +63,14 @@ namespace SqlSugar
this.InstanceCache.TryRemove(key, out val); this.InstanceCache.TryRemove(key, out val);
} }
public void RemoveAllCache()
{
foreach (var key in GetAllKey())
{
this.Remove(key);
}
}
public IEnumerable<string> GetAllKey() public IEnumerable<string> GetAllKey()
{ {
return this.InstanceCache.Keys; return this.InstanceCache.Keys;
@ -89,19 +98,19 @@ namespace SqlSugar
} }
public static class CacheManager public static class CacheManager
{ {
private static List<object> CacheObjects = new List<object>(); private static List<Action> removeActions = new List<Action>();
internal static void Add(object CacheObject) internal static void Add(Action removeAction)
{ {
CacheObjects.Add(CacheObject); removeActions.Add(removeAction);
} }
public static void RemoveAllCache() public static void RemoveAllCache()
{ {
lock (CacheObjects) lock (removeActions)
{ {
for (int i = 0; i < CacheObjects.Count; i++) foreach (var item in removeActions)
{ {
CacheObjects[i] = Activator.CreateInstance(CacheObjects[i].GetType(), true); item();
} }
} }
} }

View File

@ -188,6 +188,11 @@ namespace SqlSugar
CacheManager.RemoveAllCache(); CacheManager.RemoveAllCache();
} }
public void RemoveCacheAll<T>()
{
CacheManager<T>.GetInstance().RemoveAllCache();
}
public void RemoveCache<T>(string key) public void RemoveCache<T>(string key)
{ {
CacheManager<T>.GetInstance().Remove(key); CacheManager<T>.GetInstance().Remove(key);

View File

@ -17,6 +17,9 @@ namespace SqlSugar
T TranslateCopy<T>(T sourceObject); T TranslateCopy<T>(T sourceObject);
dynamic DataTableToDynamic(DataTable table); dynamic DataTableToDynamic(DataTable table);
ICacheManager<T> GetCacheInstance<T>(); ICacheManager<T> GetCacheInstance<T>();
void RemoveCacheAll();
void RemoveCacheAll<T>();
void RemoveCache<T>(string key);
} }
} }