[Navigate(NavigateType.OneToOne, nameof(SchoolId))]

public School School { get; set; } = new NavigationInitializer<School>();//support new
This commit is contained in:
sunkaixuan
2025-02-19 19:38:02 +08:00
parent 5ce28d2b23
commit b7ac1bf753
2 changed files with 72 additions and 0 deletions

View File

@@ -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);

View 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];
}
}
}