编辑操作不首先在 MVC 代码中将引用的 ApplicationUser Id 写入数据库
Edit Action not writing referenced ApplicationUser Id to DB in MVC code first
我在我的所有表中都引用了 ASP.NET Identity ApplicationUser 模型 (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
class不包含LastUpdatedByUser
导航属性的任何ForeigKey
, 所以首先更新你的 ConsignmentStatus
class 如下:
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);
}
希望它现在对你有用!
我在我的所有表中都引用了 ASP.NET Identity ApplicationUser 模型 (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
class不包含LastUpdatedByUser
导航属性的任何ForeigKey
, 所以首先更新你的 ConsignmentStatus
class 如下:
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);
}
希望它现在对你有用!