简体   繁体   English

实体框架代码首先更新复杂类型的单个属性

[英]Entity Framework Code First Update a Single Property of a Complex Type

I am having a weird behavior. 我有一种奇怪的行为。 I want to update a single property of a complex type. 我想更新复杂类型的单个属性。 When I specify properties to be updated with IsModified (some property are to true and some to false) I have nothing updated. 当我指定要使用IsModified更新的属性(某些属性为true而某些属性为false)时,我没有更新任何内容。 If I specify no property of the complex type, every field of the complex property are updated. 如果我未指定复杂类型的属性,那么将更新复杂属性的每个字段。

public class MyEntity
{
    public MyComplexClass Property1 { get; set; }
}

//... The code below doesn't work, in fact it update nothing
var entityLocal = this.Set<MyEntity>().Local.SingleOrDefault(d => d.Id == entity.Id);
if (entityLocal == null)
{
   entityLocal = this.Set<MyEntity>().Attach(entity);
}
this.ChangeTracker.Entries<MyEntity>().Single(d => d.Entity == entityLocal).State = EntityState.Modified;
this.Entry(entity).Property(s => s.Property1.SubProperty1).IsModified = true;
this.Entry(entity).Property(s => s.Property1.SubProperty2).IsModified = false;//This seam to remove all update of the complex type...?
this.SaveChanges();

This produce : 这产生:

update [dbo].[MyEntity]
set @p = 0
where (([Id] = @0))

If I do not specify the IsModified to false of the SubProperty2, I have the following in the SQL profiler: 如果我没有将SubMperty2的IsModified指定为false,我在SQL事件探查器中有以下内容:

update [dbo].[MyEntity]
set [Property1_SubProperty1] = @0, [Property1_SubProperty2] = null
where (([Id] = @1))

How come when I specify "IsModified" on some property that nothing is updated? 当我在没有任何内容更新的某些属性上指定“ IsModified”时,怎么会这样?

Edit 编辑

After several try, I can confirm that if I check with those two lines that it's when 1 property of a complex type is set to IsModified to False that the whole complex type is no updated. 经过多次尝试,我可以确认,如果我检查这两行,当复杂类型的1个属性设置为IsModified为False时,整个复杂类型没有更新。

var entry = DatabaseContext.Entry(entity);
var namesOfChangedProperties = entry.CurrentValues.PropertyNames.Where(p => entry.Property(p).IsModified).ToArray();

If I set to True any property, no problem but when 1 property is set to false (IsModified), the whole SubProperty is not inside the namesOfChangedProperties variable. 如果我将任何属性设置为True,没有问题但是当1属性设置为false(IsModified)时,整个SubProperty不在namesOfChangedProperties变量中。

Edit 2 编辑2

I have try to use ComplexProperty with the same result. 我尝试使用ComplexProperty具有相同的结果。

this.ChangeTracker.Entries<MyEntity>().Single(d => d.Entity == entityLocal).State = EntityState.Modified;
this.Entry(entity).ComplexProperty(s => s.Property1).Property(d => d.SubProperty1).IsModified = true;
this.Entry(entity).ComplexProperty(s => s.Property1).Property(d => d.SubProperty2).IsModified = false;
this.SaveChanges();

This is a limitation of the way EF does change tracking of complex types. 这是EF改变复杂类型跟踪方式的限制。 EF doesn't track changes at the property level, but rather only tracks that the whole object is modified or not. EF不会在属性级别上跟踪更改,而只会跟踪整个对象是否已修改。 This is a limitation that was built in as part of EF1 and has not been removed. 这是作为EF1的一部分构建的限制,尚未删除。 On the one hand it would be good to remove this limitation for those people who do want more granular change tracking. 一方面,对于那些希望进行更详细的变更跟踪的人来说,消除此限制将是一件好事。 However, on the other hand it is often considered best practice to treat complex objects as immutable "value types". 但是,另一方面,将复杂对象视为不可变的“值类型”通常被认为是最佳实践。 For such value types the whole object is always set, which makes more granular change tracking less useful. 对于此类值类型,始终设置整个对象,这使得更细粒度的更改跟踪变得不那么有用。 Also, more granular change tracking is not a very commonly requested feature so it is unlikely that the EF team would work on it soon. 同样,更细粒度的变更跟踪也不是普遍要求的功能,因此EF团队不太可能很快对其进行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先:类型类型的计算属性导致Update-Database上的ArgumentNullException - Entity Framework Code First: Computed property of type Type causes ArgumentNullException on Update-Database 实体框架4.3-代码优先-更新列表属性 - Entity Framework 4.3 - Code First - Update List Property 代码优先 类型 '' 的属性 '' 上的 ForeignKeyAttribute 无效。 实体框架 - Code First The ForeignKeyAttribute on property '' on type '' is not valid. Entity Framework 实体框架复杂类型验证(数据库优先) - Entity Framework Complex Type Validation (DB First) 实体框架中复杂类型属性名称的别名 - Aliases for Complex Type Property Names in the Entity Framework 实体框架复杂类型属性作为键 - Entity Framework Complex Type property as key 实体框架代码优先-实体或复杂类型不能在LINQ to Entities查询中构造 - Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query 带有实体框架的ASP.NET MVC - 更新/保存复杂类型属性 - ASP.NET MVC with Entity Framework - Update/Save complex type property 如何更新 Entity Framework Core 中的单个属性 - How to update a single property in Entity Framework Core 实体框架代码优先 - 为什么我不能这样更新复杂属性? - Entity Framework Code First - Why can't I update complex properties this way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM