Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较复杂对象并获得不同的C.NET Core 3.1_C#_Asp.net Core_Entity Framework Core_Compare - Fatal编程技术网

C# 比较复杂对象并获得不同的C.NET Core 3.1

C# 比较复杂对象并获得不同的C.NET Core 3.1,c#,asp.net-core,entity-framework-core,compare,C#,Asp.net Core,Entity Framework Core,Compare,我有两个类配置文件的对象,我想比较它以获得差异。为了更好地解释: public partial class Profile { public long Id { get; set; } public long VisitorId { get; set; } public string Description { get; set; } public long Age { get; set; } pub

我有两个类配置文件的对象,我想比较它以获得差异。为了更好地解释:

    public partial class Profile
    {
        public long Id { get; set; }
        public long VisitorId { get; set; }
        public string Description { get; set; }
        public long Age { get; set; }
        public DateTime? LastUpdate { get; set;}
    }
我想在我的方法中知道对象oldProfile和newProfile之间的差异,以生成变更日志

例如,如果oldProfile的年龄=10,描述=old,而newProfile的年龄=11,描述=new,那么我现在可以利用这些差异在数据库中进行两次不同的插入:

public void PostChangelogProfileDetail(Profile oldProfile, Profile newProfile)
{
    ProfileDetailChangeLog profileDetailChangeLog = new ProfileDetailChangeLog();

    //COMPARE oldProfile AND newProfile

    foreach ( //difference resulted in the compare)
    {
        profileDetailChangeLog.VisitorId = newProfile.VisitorId;
        profileDetailChangeLog.ModifiedRecord = //name of the attribute modified (Age, Description, etc...)

        _profileDetailChangeLog.Create(profileDetailChangeLog);
    }
}

您可能希望使用EF Cores元数据来确定它正在跟踪的对象的更改。我自己在.NETCore3中做了一些类似于stackoverflow的解决方案,这让我相信3.1也应该可以工作

以下文章介绍了实现这一点的方法:
&.

您可能希望使用EF Cores元数据来确定跟踪对象的更改。我自己在.NETCore3中做了一些类似于stackoverflow的解决方案,这让我相信3.1也应该可以工作

以下文章介绍了实现这一点的方法:
&.

从nugget安装软件包 安装包ObjectsComparer

然后进行比较: var cmpr=新比较器; /*比较 isSame是booleab变量,如果两个对象相同,则返回true */ i可数差


bool isname=cmpr.Compareobj1,obj2,out diff

从nugget安装软件包 安装包ObjectsComparer

然后进行比较: var cmpr=新比较器; /*比较 isSame是booleab变量,如果两个对象相同,则返回true */ i可数差

bool isname=cmpr.Compareobj1,obj2,out diff

当您调用或ef core返回时。 从哪个EntityEntry可以通过OriginalValues返回旧值,CurrentValues返回新值属性获取当前值和旧值

从这里,您可以通过反射比较和记录原始值和当前值的差异,以查看属性的类型/名称以及返回类型。

调用或ef core返回时。 从哪个EntityEntry可以通过OriginalValues返回旧值,CurrentValues返回新值属性获取当前值和旧值


在这里,您可以通过反射比较和记录原始值和当前值的差异,以查看属性的类型/名称以及返回类型的值。

对于断开连接的实体,我发现了这一点

要查找现有实体上的更改,请执行以下操作:

var existing = context.Find<Item>(1);
if (existing != null) 
{ 
   context.Entry(existing).CurrentValues.SetValues(changed);
}
其EntityState将在以后进行修改,但仅在发生实际更改的情况下进行修改


发布了相同的答案。

对于断开连接的实体,我发现了这个

要查找现有实体上的更改,请执行以下操作:

var existing = context.Find<Item>(1);
if (existing != null) 
{ 
   context.Entry(existing).CurrentValues.SetValues(changed);
}
其EntityState将在以后进行修改,但仅在发生实际更改的情况下进行修改


发布了相同的答案。

比较对象有多种方法1编写代码逐属性比较2使用库;例如,您可以使用Json,使用jsondiffpatch.net查找差异。检查这个答案-有多种方法来比较对象1编写代码以逐属性比较属性2使用库;例如,您可以使用Json,使用jsondiffpatch.net查找差异。检查这个答案-谢谢你的回答,我会记住这个提示。看看我关于如何不必自己查询CurrentValue的答案。谢谢你的回答,我会记住这个提示。看看我关于如何不必自己查询CurrentValue的答案。我不知道这件事,我肯定将来会考虑这种方法。不知道这件事,我肯定会在以后考虑这种方法。非常感谢,我看到这个包裹,它是完美的为我的需要。它也可以免费使用,对吗?我很喜欢这个框架,但是如果你想更新你的数据库,它有点多余。非常感谢,我看到了这个软件包,它非常适合我的需要。它也可以免费使用,对吗?非常喜欢这个框架,但是如果你想更新你的数据库,它有点多余。