实体框架-仅更新不为null的值
本文关键字:null 的值 更新 框架 实体 | 更新日期: 2023-09-27 18:20:21
这对我来说有点新。我被要求编写一个ETL程序,将两个数据集加载到同一个表中。数据集#1是完整的,包含表的所有数据。然而,数据集#2只包含需要覆盖到第一个数据集上的更改。观察:
//数据集#1:小工具表
+----+------+------+------+------+
| ID | COL1 | COL2 | COL3 | COL4 |
+----+------+------+------+------+
| 1 | abcd | abcd | abcd | abcd |
+----+------+------+------+------+
| 2 | abcd | abcd | abcd | abcd |
+----+------+------+------+------+
//数据集#2:Widgets_Changes表
+----+------+------+------+------+
| ID | COL1 | COL2 | COL3 | COL4 |
+----+------+------+------+------+
| 1 | | efgh | | ijkl |
+----+------+------+------+------+
| 2 | mnop | | qrst | |
+----+------+------+------+------+
//预期结果:所有更改的小工具
+----+------+------+------+------+
| ID | COL1 | COL2 | COL3 | COL4 |
+----+------+------+------+------+
| 1 | abcd | efgj | abcd | ijkl |
+----+------+------+------+------+
| 2 | mnop | abcd | qrst | abcd |
+----+------+------+------+------+
显而易见的方法(我试图避免)是从第一个表中取出每个小部件,并进行逐个属性的比较:
// Simplified example:
using ( var db = new MyEntityDatabase() ){
var widget = from p in db.Widgets select p where p.ID == 1;
var widget_diff = from p in db.Widgets_Changes select p where p.ID == 1
widget.COL1 = widget_diff.COL1 ?? widget.COL1;
widget.COL2 = widget_diff.COL2 ?? widget.COL2;
widget.COL3 = widget_diff.COL3 ?? widget.COL3;
// ...etc
db.saveChanges();
}
然而,在这个特定的数据集中有200多个字段,更多的文件遵循相同的方法(完整的数据集伴随着diff数据集),但具有完全不同的模式。显然,我更希望有一些可移植的东西,我可以直接运行文件,而不必为每个数据集逐个硬编码属性比较。
有没有一种方法可以遍历这两个对象的属性并更新不为null的值?
首先,您需要使用类似的东西来选择要更新的实体:
var widget = db.Widgets.First(p => p.ID == 1);
var widget_diff = db.Widgets_Changes.First(p => p.ID == 1);
现在您可以简单地使用反射来更新所有字段:
foreach(var toProp in typepf(Widget).GetProperties())
{
var fromProp= typeof(Widget_Change).GetProperty(toProp.Name);
var toValue = fromProp.GetValue(widget_diff, null);
if (toValue != null)
{
toProp.SetValue(widget, toValue, null);
}
}
这可以通过提前建立一个属性列表来加快速度,所以你只需要使用一次反射:
public static class WidgetUtil
{
public static readonly IEnumerable<Tuple<PropertyInfo, PropertyInfo>> PropertyMap;
static Util()
{
var b = BindingFlags.Public | BindingFlags.Instance;
PropertyMap =
(from f in typeof(Widget).GetProperties(b)
join t in typeof(WidgetChange).GetProperties(b) on f.Name equals t.Name
select Tuple.Create(f, t))
.ToArray();
}
}
...
foreach(var propertyPair in WidgetUtil.PropertyMap)
{
var toValue = propertyPair.Item2.GetValue(widget_diff, null);
if (toValue != null)
{
propertyPair.Item1.SetValue(widget, toValue, null);
}
}
如果你有很多这样的实体类型,你甚至可能想考虑把它变成一个通用的实用程序:
public static class WidgetUtil<T1, T2>
{
public static readonly IEnumerable<Tuple<PropertyInfo, PropertyInfo>> PropertyMap;
static WidgetUtil()
{
var b = BindingFlags.Public | BindingFlags.Instance;
PropertyMap =
(from f in typeof(T1).GetProperties(b)
join t in typeof(T2).GetProperties(b) on f.Name equals t.Name
select Tuple.Create(f, t))
.ToArray();
}
}
您可能需要使用反射。循环遍历每个小部件/差异的所有属性/字段,获取该属性/字段的值,如果差异为null,则使用原始值。
using(var db = new MyEntityDatabase())
{
var widget = from p in db.Widgets select p where p.ID == 1;
var widget_diff = from p in db.Widgets_Changes select p where p.ID == 1;
var properties = typeof(MyWidgetType).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(var property in properties)
{
//widget.column = widget_diff.column ?? widget.colum;
property.SetValue(property.GetValue(widget_diff) ?? property.GetValue(widget), widget);
}
//You can do the same for fields here if the entity has any fields (probably not).
}
@p.s.w.g的答案很好,但当我尝试实现它时,我遇到了几个错误(例如,你不能用obj.Equals(null)检查null,null没有Equals方法)。
以下是@p.s.w.g的伟大答案(作为副产品)的"完整副本可粘贴解决方案"
静态泛型方法InjectNonNull
获取要更新的源实体和具有null的目标"sparce"实体,并仅传输目标实体上的非null属性。
private static class PropertyLister<T1, T2>
{
public static readonly IEnumerable<Tuple<PropertyInfo, PropertyInfo>> PropertyMap;
static PropertyLister()
{
var b = BindingFlags.Public | BindingFlags.Instance;
PropertyMap =
(from f in typeof(T1).GetProperties(b)
join t in typeof(T2).GetProperties(b) on f.Name equals t.Name
select Tuple.Create(f, t))
.ToArray();
}
}
public static T InjectNonNull<T>(T dest, T src)
{
foreach (var propertyPair in PropertyLister<T, T>.PropertyMap)
{
var fromValue = propertyPair.Item2.GetValue(src, null);
if (fromValue != null && propertyPair.Item1.CanWrite)
{
propertyPair.Item1.SetValue(dest, fromValue, null);
}
}
return dest;
}