编辑操作没有首先在MVC代码中将引用的ApplicationUser Id写入DB



我已经引用了ASP。NET标识应用程序我所有表中的用户模型(LastUpdatedByUser)

当我尝试在Edit方法中将其设置为当前用户时,对象属性设置正确,但生成的SQL在UPDATE查询中不包括LastUpdateByUser_Id。

型号属性:

public class ConsignmentStatus
{
//...
[Required]
[Display(Name = "UpdatedBy", ResourceType = typeof(Resources.Resources))]
public ApplicationUser LastUpdatedByUser { get; set; }
}

控制器上的编辑操作:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ID,Code,Name,Description,IsTruckInbound,IsRailInbound,IsVesselOutbound,IsCarImport,IsBulkImport,IsTruckOutbound,NameLocal,LastUpdatedByUser")] ConsignmentStatus consignmentStatus)
{
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));
var _user = (from u in db.Users
where u.UserName == User.Identity.Name
select u).First();
consignmentStatus.LastUpdatedByUser = _user;
ModelState.Clear();
TryValidateModel(consignmentStatus);
if (ModelState.IsValid)
{
db.Entry(consignmentStatus).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(consignmentStatus);
}

我错过了什么?我什么都试过了!

这是它生成的查询。注意缺少LastUpdatedByUser_Id:

UPDATE [dbo].[ConsignmentStatus]
SET [Code] = @0, [Name] = @1, [NameLocal] = @2, [Description] = NULL, 
[IsTruckInbound] = @3, [IsRailInbound] = @4, [IsVesselOutbound] = @5, 
[IsCarImport] = @6, [IsBulkImport] = @7, [IsTruckOutbound] = @8, 
[LastUpdatedDateTime] = @9, [Deleted] = @10
WHERE ([ID] = @11)
-- @0: 'WA' (Type = String, Size = -1)
-- @1: 'Wagons Arrived' (Type = String, Size = -1)
-- @2: '1232412312' (Type = String, Size = -1)
-- @3: 'False' (Type = Boolean)
-- @4: 'True' (Type = Boolean)
-- @5: 'False' (Type = Boolean)
-- @6: 'False' (Type = Boolean)
-- @7: 'False' (Type = Boolean)
-- @8: 'False' (Type = Boolean)
-- @9: '10/01/2019 12:29:32' (Type = DateTime2)
-- @10: 'False' (Type = Boolean)
-- @11: '24' (Type = Int32)

正如您所说的您的ConsignmentStatus类不包含任何用于LastUpdatedByUser导航属性的ForeigKey,因此首先按如下方式更新ConsignmentStatus类:

public class ConsignmentStatus
{
//...
[ForeignKey("LastUpdatedByUser")]
public string LastUpdatedByUserId { get; set; } // type should be the type of `ApplicationUser` primary key. But I have put it string here.
[Required]
[Display(Name = "UpdatedBy", ResourceType = typeof(Resources.Resources))]
public ApplicationUser LastUpdatedByUser { get; set; }
}

现在相应地更新数据库。

然后尝试将userId分配给LastUpdatedByUserId,而不是将LastUpdatedByUser分配给导航属性LastUpdatedByUser,如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ID,Code,Name,Description,IsTruckInbound,IsRailInbound,IsVesselOutbound,IsCarImport,IsBulkImport,IsTruckOutbound,NameLocal,LastUpdatedByUser")] ConsignmentStatus consignmentStatus)
{
ModelState.Clear();
TryValidateModel(consignmentStatus);
if (ModelState.IsValid)
{
var loggedinUserId = User.Identity.GetUserId();
consignmentStatus.LastUpdatedByUserId = loggedinUserId; // assgin userId to `LastUpdatedByUserId` in `consignmentStatus`
db.Entry(consignmentStatus).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(consignmentStatus);
}

希望它现在对你有用!

相关内容

  • 没有找到相关文章

最新更新