小编Joo*_*nen的帖子

如何通过值转换修改属性上的 EntityState?

我使用 ValueConverter 将自定义属性类型(列表)映射到字符串。转换工作完美,但我无法将 EntityState 设置为已修改,因为 EF 找不到实体。

当我修改列表时,EF 不会检测到更改,这是 expected。但是,当我尝试手动更改状态时,出现错误

The entity type 'List<List<Reward>>' was not found. Ensure that the entity type has been added to the model.
Run Code Online (Sandbox Code Playgroud)

我发现的唯一解决方法是将属性设置为列表的克隆,这并不理想。

model.PropertyName = new List<Reward>(model.PropertyName); // clone the list
Run Code Online (Sandbox Code Playgroud)

下面是一些示例代码:

public class RewardContainer
{
    public List<List<Reward>> Rewards { get; set; }
}

// db context
protected override void OnModelCreating(ModelBuilder builder)
{
    // use custom conversion, which is working fine
    builder.Entity<RewardContainer>().Property(p => p.Rewards).HasConversion(ValueConverters.GetJsonConverter<List<List<Reward>>>());
}

// controller
public async Task ModifyProperty()
{
    rewardContainer.Rewards[0].Add(someReward);

    // …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core asp.net-core

0
推荐指数
1
解决办法
631
查看次数