如何在aspnetboilerplate中更新实体?

时间:2018-07-11 02:13:18

标签: aspnetboilerplate

我想更新数据库中的实体。我使用aspnetboilerplate模板项目。我在应用程序层中有一个方法UpdateAsset

public async Task UpdateAsset(UpdateAssetInput input)
    {
        var asset = ObjectMapper.Map<Asset>(input.Asset);
        asset.Domain = asset.Domain.ToLowerInvariant();

        // Update Twitter Id
        var twitterName = input.Asset.SocialAccounts?.TwitterInfo?.Name;
        if (twitterName != null)
        {
            var twitterId = await _twitterActivityManager.GetTwitterIdByTwitterName(twitterName);
            if (twitterId != null)
            {
                input.Asset.SocialAccounts.TwitterInfo.Id = twitterId;
            }
        }

        asset.SetData<SocialAccounts>(AssetExtensionData.SocialAccounts, input.Asset.SocialAccounts);
        var connectedAsset = await _assetManager.GetAsset(input.Asset.LockedPositionInfo.ConnectedAssetId);
        if (connectedAsset != null)
        {
            input.Asset.LockedPositionInfo.ConnectedAssetUnit = connectedAsset.Unit;
        }
        asset.SetData<LockedPositionInfo>(AssetExtensionData.LockedPositionInfo, input.Asset.LockedPositionInfo);
        asset.SetData(AssetExtensionData.WithdrawalApiInfo, input.Asset.WithdrawalApiInfo);
        await _assetManager.UpdateAsset(asset);
    }

UpdateAssetInput:

public class UpdateAssetInput
{
    public AssetDto Asset { get; set; }
}

AssetDto:

[AutoMap(typeof(Asset))]
public class AssetDto : AuditedEntityDto<string>
{
    public const int SYMBOL_LENGTH = 10;

    [Required]
    [MaxLength(SYMBOL_LENGTH)]
    public new string Id { get; set; }

    [Required]
    public string Name { get; set; }

    public string Description { get; set; }

    public string Website { get; set; }

    [Required]
    public string Domain { get; set; }

    public string Logo { get; set; }

    public string Organization { get; set; }

    public string Unit { get; set; }

    public SocialAccounts SocialAccounts { get; set; }

    public LockedPositionInfo LockedPositionInfo { get; set; }

    public WithdrawalApiInfo WithdrawalApiInfo { get; set; }


    public decimal TotalAmount { get; set; }

    public decimal Price { get; set; }

    public bool IsDisable { get; set; } = false;
}

AssetManager中的UpdateAsset:

public async Task UpdateAsset(Asset asset)
    {
        try
        {
            await _assetRepository.UpdateAsync(asset);
        }
        catch (Exception e)
        {
            Logger.Error(e.Message, e);
            throw new UserFriendlyException(L("AssetUpdateFailed"), asset.Name);
        }
    }

当我在前端调用应用程序层的UpdateAsset时,出现异常:

System.InvalidOperationException: 'The instance of entity type 'Asset' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. 

那么如何解决问题呢?

1 个答案:

答案 0 :(得分:0)

通常,当我们更新资产实体时,我们将执行以下3个步骤:

  1. 从数据库获取资产实体
  2. 将更新后的值设置为资产实体
  3. 更新资产实体

根据您的错误消息,您可能两次从数据库获得资产实体

  1. 从数据库获取资产1实体-跟踪
  2. 从数据库获取资产2实体-跟踪
  3. 将更新后的值设置为asset2实体
  4. 更新asset2实体

请检查您是否有上述代码段