mh_frame_sps/APT.Infrastructure.EF/SimpleEntityScaner.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2026-04-07 13:47:52 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace APT.Infrastructure.EF
{
public class SimpleEntityScaner<TDbContext> : IEntityScaner<TDbContext>
where TDbContext : DbContext
{
private static List<DbsetProperty> dbProperties;
private static object locker = new object();
public IList<DbsetProperty> ScanEntityTypes()
{
if (dbProperties == null)
{
lock (locker)
{
if (dbProperties == null)
{
var contextType = typeof(TDbContext);
var properties = contextType.GetProperties();
var entityProperties = properties.Where(r => IsDbSet(r)).ToList();
dbProperties = new List<DbsetProperty>();
foreach (var property in entityProperties)
{
dbProperties.Add(new DbsetProperty
{
PropertyName = property.Name,
PropertyType = property.PropertyType.GetGenericArguments()[0]
});
}
}
}
}
return dbProperties;
}
bool IsDbSet(PropertyInfo property)
{
if (property.CanRead && property.PropertyType.IsGenericType
&& typeof(DbSet<>).GetGenericTypeDefinition().Equals(property.PropertyType.GetGenericTypeDefinition()))
{
return true;
}
return false;
}
}
}