c# - 无法检测 EF Core 中阴影属性的更改
问题描述
我正在尝试更改属性,但是阴影属性存在问题,两者CurrentValue
都OriginalValue
为空,是否有阴影属性的替代方法?
var entities = _context
.ChangeTracker
.Entries()
.ToArray();
foreach (var entity in entities)
{
foreach (var property in entity.Properties)
{
if (property.Metadata.IsShadowProperty())
{
var newValue = property.CurrentValue; // null
var oldValue = property.OriginalValue; // null
}
}
}
实体类:
public class ParentEntity
{
public string Id { get; set; }
public OwnedEntity SomeEntity { get; set; }
private string _name;
public ParentEntity(string id, string name)
{
Id = id;
_name = name;
SomeEntity = new OwnedEntity();
}
}
public class OwnedEntity
{
public string Id { get; set; }
public string Name { get; set; }
public OwnedEntity()
{
Id = Guid.NewGuid().ToString();
Name = "Some Name";
}
}
配置:
public void Configure(EntityTypeBuilder<ParentEntity> builder)
{
builder.HasPartitionKey(x => x.PartitionKey);
builder.Property("_name").ToJsonProperty("name");
builder.Property(x => x.Id).ToJsonProperty("id");
builder.OwnsOne(x => x.SomeEntity, x =>
{
x.ToJsonProperty("someEntity");
x.Property(x => x.Id).ToJsonProperty("id");
x.Property(x => x.Name).ToJsonProperty("name");
});
builder.HasKey(x => x.Id);
builder.HasDiscriminator();
builder.ToContainer("container1");
}
解决方案
推荐阅读
- android - 线程“queued-resource-processor_1”中出现异常的原因是什么?
- database - MongoDB - 查询嵌入式文档数组
- javascript - 在 Web Worker 中访问串行端口
- apache-spark - 如何在 Spark 和 Cassandra 中将数据从较小的表映射到较大的表?(第一表的主键是第二表的分区键)
- authentication - 使用 OAuth 集成产品
- ios - 限制两个特定文本字段中的字符数
- amazon-ec2 - 我可以为 ECS 使用现有的、正在使用的 EC2 实例吗?
- javascript - 使用 JavaScript 或 jQuery 在函数中包装或不包装
- python - 在 gimpfu 中使用外部 GUI 撤消
- php - PHP json_decode 由于 json 元素