当我想将可为null的Int32值从null修补为具体值时,它引发“无法将类型为'System.Int64'的对象转换为类型为'System.Nullable`1 [System.Int32]”。” < / p>
尽管当我将其从具体值打补丁为null时,它仍然可以正常工作。 Int64是键,int是可为空的属性。
模型:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, Column(Order = 0)]
public Int64 Id { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
修补方法:
public async Task ApplyPatchAsync<TEntity>(TEntity entityName,
List<PatchDto> patchDtos) where TEntity : class
{
var nameValuePairProperties = patchDtos.ToDictionary(a =>
a.PropertyName, a => a.PropertyValue);
var dbEntityEntry = context.Entry(entityName);
dbEntityEntry.CurrentValues.SetValues(nameValuePairProperties);
dbEntityEntry.State = EntityState.Modified;
await context.SaveChangesAsync();
}
public class PatchDto
{
public string PropertyName { get; set; }
public object PropertyValue { get; set; }
}