using Microsoft.EntityFrameworkCore;
using System.Collections;
using System.Linq.Expressions;
using System.Text;
using System.Transactions;
using Masuit.Tools.Systems;
using Masuit.Tools.Reflection;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query;
namespace Masuit.Tools.Core;
public static class DbContextExt
{
///
/// 获取变化的实体信息
///
///
///
///
public static IEnumerable> GetChanges(this DbContext db)
{
return db.ChangeTracker.Entries().Where(e => e is { State: EntityState.Modified, Entity: T }).Select(e => new ChangeEntry
{
EntityState = e.State,
Entity = (T)e.Entity,
#if NET10_0_OR_GREATER
EntityType = e.OriginalValues.StructuralType.ClrType,
#else
EntityType = e.OriginalValues.EntityType.ClrType,
#endif
ChangeProperties = e.OriginalValues.Properties.Select(p => (Property: p, Value: e.OriginalValues[p])).Zip(e.CurrentValues.Properties.Select(p => (Property: p, Value: e.CurrentValues[p])), (t1, t2) => new ChangePropertyInfo
{
PropertyInfo = t1.Property.PropertyInfo,
OriginalValue = t1.Value,
CurrentValue = t2.Value,
IsPrimaryKey = t1.Property.IsPrimaryKey(),
IsForeignKey = t1.Property.IsForeignKey()
}).Where(t =>
{
if (t.OriginalValue is IEnumerable