mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 修复 DbContext DbSet 属性初始并发时未触发 OnModelCreating;#1333
This commit is contained in:
parent
bb57b1ffd2
commit
fe5a8fc355
@ -83,20 +83,29 @@ namespace FreeSql
|
||||
|
||||
#region Set
|
||||
static ConcurrentDictionary<Type, NativeTuple<PropertyInfo[], bool>> _dicGetDbSetProps = new ConcurrentDictionary<Type, NativeTuple<PropertyInfo[], bool>>();
|
||||
static object _lockOnModelCreating = new object();
|
||||
internal void InitPropSets()
|
||||
{
|
||||
var thisType = this.GetType();
|
||||
var dicval = _dicGetDbSetProps.GetOrAdd(thisType, tp =>
|
||||
NativeTuple.Create(
|
||||
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||
var isOnModelCreating = false;
|
||||
if (_dicGetDbSetProps.TryGetValue(thisType, out var dicval) == false)
|
||||
{
|
||||
lock (_lockOnModelCreating)
|
||||
{
|
||||
if (_dicGetDbSetProps.TryGetValue(thisType, out dicval) == false)
|
||||
{
|
||||
dicval = NativeTuple.Create(
|
||||
thisType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||
.Where(a => a.PropertyType.IsGenericType &&
|
||||
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GetGenericArguments()[0])).ToArray(),
|
||||
false));
|
||||
if (dicval.Item2 == false)
|
||||
{
|
||||
if (_dicGetDbSetProps.TryUpdate(thisType, NativeTuple.Create(dicval.Item1, true), dicval))
|
||||
OnModelCreating(OrmOriginal.CodeFirst);
|
||||
false);
|
||||
_dicGetDbSetProps.TryAdd(thisType, dicval);
|
||||
isOnModelCreating = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isOnModelCreating)
|
||||
OnModelCreating(OrmOriginal.CodeFirst);
|
||||
|
||||
foreach (var prop in dicval.Item1)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user