mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
[Navigate(NavigateType.OneToOne, nameof(SchoolId))]
public School School { get; set; } = new NavigationInitializer<School>();//support new
This commit is contained in:
@@ -392,6 +392,17 @@ namespace SqlSugar
|
||||
FieldValue = String.Join(",", ids),
|
||||
CSharpTypeName = navPkColumn?.UnderType?.Name
|
||||
}));
|
||||
if (NavigationGlobalInstanceRegistry.IsAny())
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
var firstObj = navObjectNamePropety.GetValue(item);
|
||||
if (NavigationGlobalInstanceRegistry.IsNavigationInitializerCreated(firstObj))
|
||||
{
|
||||
navObjectNamePropety.SetValue(item,null);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (list.Any()&&navObjectNamePropety.GetValue(list.First()) == null)
|
||||
{
|
||||
var sqlObj = GetWhereSql(db,navObjectNameColumnInfo.Navigat.Name);
|
||||
|
61
Src/Asp.NetCore2/SqlSugar/Entities/NavigationInitializer.cs
Normal file
61
Src/Asp.NetCore2/SqlSugar/Entities/NavigationInitializer.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Reflection.Emit;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal static class NavigationGlobalInstanceRegistry
|
||||
{
|
||||
private static readonly Dictionary<Type, object> _instances = new Dictionary<Type, object>();
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public static Dictionary<Type, object> Instances => _instances;
|
||||
|
||||
public static bool IsAny()
|
||||
{
|
||||
return _instances?.Count>0;
|
||||
}
|
||||
public static bool IsNavigationInitializerCreated(object instance)
|
||||
{
|
||||
if (instance == null)
|
||||
return false;
|
||||
|
||||
Type type = instance.GetType();
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
return _instances.ContainsKey(type) && _instances[type] == instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterInstance(Type type, object instance)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_instances.ContainsKey(type))
|
||||
{
|
||||
_instances[type] = instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NavigationInitializer<T> where T : new()
|
||||
{
|
||||
public static implicit operator T(NavigationInitializer<T> initializer)
|
||||
{
|
||||
Type type = typeof(T);
|
||||
|
||||
if (!NavigationGlobalInstanceRegistry.Instances.ContainsKey(type))
|
||||
{
|
||||
T instance = new T();
|
||||
NavigationGlobalInstanceRegistry.RegisterInstance(type, instance);
|
||||
}
|
||||
|
||||
return (T)NavigationGlobalInstanceRegistry.Instances[type];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user