gpt4 book ai didi

c# - 如何防止 Automapper 替换 Entity Framework 拥有的实体?

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:11 26 4
gpt4 key购买 nike

我有两种类型。位置和位置有一个地址。地址指定为 owned entity使用

class LocationConfiguration : IEntityTypeConfiguration<Location>
{
public void Configure(EntityTypeBuilder<Location> builder)
{
builder.HasKey(location => new { location.SubscriptionId, location.Id });
builder.OwnsOne(location => location.Address);
}
}

我正在获取现有位置实体并使用 Automapper 映射更新值。

[HttpPut("{subscriptionId}/{locationId}")]
public async Task<IActionResult> SaveLocationAsync(string subscriptionId, long locationId, [FromBody] Location location)
{
if (location == null || location.Id != locationId || location.SubscriptionId != subscriptionId)
{
return BadRequest();
}
var dbLocation = await locations.GetLocationAsync(subscriptionId, locationId);
if (dbLocation == null)
{
return NotFound();
}
mapper.Map<Location, Location>(location, dbLocation);
return Ok(await locations.SaveAsync(dbLocation));
}

我通过调用 context.SaveChangesAsync(); 进行保存

但是我得到了错误

InvalidOperationException: The instance of entity type 'Location.Address#Address' cannot be tracked because another instance with the key value 'LocationSubscriptionId:123, LocationId:1' is already being tracked. When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first.

我怀疑 Automapper 正在替换 Location 的 Address 属性,而不是向下导航并单独替换 Address 的属性。

有没有办法让 Automapper 更精细地复制属性值?

最佳答案

您应该使用 UseDestinationValue 在所有者类型映射配置中配置此类属性:

UseDestinationValue tells AutoMapper not to create a new object for some member, but to use the existing property of the destination object.

此外,如果您像示例中那样使用自映射,请确保为每个拥有的类型创建显式自映射。

对于您的示例,所需行为的最小 AutoMapper 配置如下:

cfg.CreateMap<Address, Address>();

cfg.CreateMap<Location, Location>()
.ForMember(dest => dest.Address, opt => opt.UseDestinationValue());

关于c# - 如何防止 Automapper 替换 Entity Framework 拥有的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com