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

时间:2019-02-12 15:40:18

标签: asp.net-core entity-framework-core

我使用ValueConverter将自定义属性类型(列表)映射到字符串。转换效果很好,但是我无法将EntityState设置为Modifyed,因为EF找不到该实体。

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

The entity type 'List<List<Reward>>' was not found. Ensure that the entity type has been added to the model.

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

model.PropertyName = new List<Reward>(model.PropertyName); // clone the list

以下是一些示例代码:

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);

    // try to manually change the EntityState...
    dbContext.Entry(rewardContainer.Rewards).State = EntityState.Modified;
    // error: The entity type 'List<List<Reward>>' was not found. Ensure that the entity type has been added to the model.        

    await dbContext.SaveChangesAsync();
}

1 个答案:

答案 0 :(得分:0)

除ValueConverter之外,还使用ValueComprarer可使EF跟踪转换后的实体。

public static class ValueConversionExtensions
{
    public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder) where T : class, new()
    {
        ValueConverter<T, string> converter = new ValueConverter<T, string>
        (
            v => JsonConvert.SerializeObject(v),
            v => JsonConvert.DeserializeObject<T>(v) ?? new T()
        );

        ValueComparer<T> comparer = new ValueComparer<T>
        (
            (l, r) => JsonConvert.SerializeObject(l) == JsonConvert.SerializeObject(r),
            v => v == null ? 0 : JsonConvert.SerializeObject(v).GetHashCode(),
            v => JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(v))
        );

        propertyBuilder.HasConversion(converter);
        propertyBuilder.Metadata.SetValueConverter(converter);
        propertyBuilder.Metadata.SetValueComparer(comparer);

        return propertyBuilder;
    }
}