在第二次保存到上下文时,继续获取"在上一个操作完成之前在此上下文中启动第二个操作".

Lew*_*wis 5 .net c# asp.net-mvc entity-framework

当我第二次执行我的HttpPost表单时,我一直收到以下错误.

InvalidOperationException: A second operation started on this context before a previous operation completed. 

Any instance members are not guaranteed to be thread safe.
Run Code Online (Sandbox Code Playgroud)

我的ApplicationDbContext在我的控制器中初始化为:

public class AssetController : Controller
{
    private readonly ApplicationDbContext _context;

    public AssetController(
        ApplicationDbContext context,)
    {
        _context = context;
    }
Run Code Online (Sandbox Code Playgroud)

这是控制器中处理帖子并保存的功能:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Add(IFormFile file, AddAssetViewModel model)
    {
        if (ModelState.IsValid)
        {
            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            var assetOwnership =
                _context.AssetOwnership.SingleOrDefault(o => o.AssetOwnershipId == model.OwnershipId);

            var origin = _context.Location.SingleOrDefault(l => l.LocationId == model.OriginId);

            var currentLocation = _context.Location.SingleOrDefault(l => l.LocationId == model.CurrentLocationId);

            var division = _context.Division.SingleOrDefault(d => d.DivisionId == model.DivisionId);

            var normalAsset = model.NormalAsset == 2;

            var uploadSavePath = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads\\AssetPictures\\");

            var trackingNumber = GetTrackingNumber(model.OwnershipId, model.DivisionId);

            var asset = new Asset
            {
                TrackingNum = trackingNumber,
                Owner = currentUser,
                Ownership = assetOwnership,
                CurrentLocation = currentLocation,
                Origin = origin,
                ModelName = model.ModelName,
                SerialNum = model.SerialNum,
                Division = division,
                Desc = model.Desc,
                HwOpt = model.HwOpt,
                SwOpt = model.SwOpt,
                Availability = model.Availability,
                Remarks = model.Remarks,
                ReadyToSell = model.ReadyToSell,
                PurchaseDate = model.PurchaseDate,
                PurchasePo = model.PurchasePo,
                NormalAsset = normalAsset,
                MaterialNumber = model.MaterialNum,
                IsTagged = model.IsTagged,
                PurchasePrice = model.PurchasePrice,
                IsDamaged = model.IsDamaged,
                LastCalDate = model.LastCalDate,
                Firmware = model.Firmware,
                EstimatedNextCalDate = model.EstimatedNextCalDate,
                LicenceExpiry = model.LicenceExpiry
            };

            if (file != null)
            {
                var imageName = asset.TrackingNum + ".jpg";

                if (file.Length > 0)
                {
                    using (var fileStream =
                        new FileStream(Path.Combine(uploadSavePath, imageName), FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    asset.AssetPicture = imageName;
                }
            }

            _context.Asset.Add(asset);

            await _context.SaveChangesAsync();

            return RedirectToAction("Index");
        }
        return View(model);
}

}
Run Code Online (Sandbox Code Playgroud)

当我第一次提交表单时,一切顺利,项目正确保存到数据库中.但是,当我尝试添加第二个项目时,我收到错误.任何人都可以帮我解决这个问题吗?错误输出说它失败了

Project.Controllers.AssetController+<Add>d__14.MoveNext() in AssetController.cs
+
            await _context.SaveChangesAsync();
Run Code Online (Sandbox Code Playgroud)

Lew*_*wis 8

我终于修好了.我忘了用异步调用async做一个我的帮助方法,那些调用等待.所以搞砸了整个事情.