请参阅下面我的Model类,我的控制器操作和我的视图。 当我从我的视图编辑时,我遇到了异常:发生了参照完整性约束违规:定义参照约束的属性值在关系中的主体和依赖对象之间不一致。
我已经问过这个问题,但我没有答案;请帮助!!
public partial class Organization : ILockable, IAuditable, IEntity
{
/*** Construtor(s) ***/
public Organization()
{
}
public Organization(Party obj)
: this()
{
Party = obj;
}
/*** Public Members ***/
[Key, Display(Name = "Id")]
public int PartyId { get; set; }
/* IEntity */
public string Caption { get; set; }
public string NameInUse { get; set; }
public string Description { get; set; }
/* IAuditable */
[NotMapped, ScaffoldColumn(false)]
public System.DateTimeOffset Created
{
get { return Party.Created; }
set { Party.Created = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string CreatedBy
{
get { return Party.CreatedBy; }
set { Party.CreatedBy = value; }
}
[NotMapped, ScaffoldColumn(false)]
public Nullable<System.DateTimeOffset> LastMod
{
get { return Party.LastMod; }
set { Party.LastMod = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string LastModBy
{
get { return Party.LastModBy; }
set { Party.LastModBy = value; }
}
[NotMapped, Display(Name = "Del?")]
public bool IsSoftDeleted
{
get { return Party.IsSoftDeleted; }
set { Party.IsSoftDeleted = value; }
}
[NotMapped, ScaffoldColumn(false)]
public Nullable<System.DateTimeOffset> SoftDeleted
{
get { return Party.SoftDeleted; }
set { Party.SoftDeleted = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string SoftDeletedBy
{
get { return Party.SoftDeletedBy; }
set { Party.SoftDeletedBy = value; }
}
/* ILockable */
public string GetTableName()
{
return "Organization";
}
public int GetLockId()
{
return this.PartyId;
}
/* Navigation Properties */
/// <summary>
/// Foreign key to Party: PartyId
/// Organization is subtype of Party
/// </summary>
public virtual Party Party { get; set; }
}
控制器编辑操作:
[HttpPost]
public ActionResult Edit(Organization obj)
{
//remove the lock since it is not required for inserts
if (ModelState.IsValid)
{
OrganizationRepo.Update(obj);
UnitOfWork.Save();
LockSvc.Unlock(obj);
return RedirectToAction("List");
}
else
{
return View();
}
}
查看: @using PartyBiz.Models.Objects @using d2Utils.Reflection
@model IEnumerable<Organization>
@{
ViewBag.Title = "Details";
}
<table>
<tr>
<th>
@Html.Raw("Caption")
</th>
<th></th>
</tr>
<tr>
<td colspan="4">
@foreach (var item in Model)
{
<table>
<tr>
@using (Html.BeginForm("Edit", "Organization", FormMethod.Post))
{
<td >
@Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { @class = "txt" })
</td>
<td >
@Html.TextBox("NameInUse", item.GetValForProp<string>("NameInUse"), new { @class = "txt" })
</td>
<td >
@Html.TextBox("Description", item.GetValForProp<string>("Description"), new { @class = "txt" })
</td>
<td>
<input type="hidden" name="PartyId" value="@item.PartyId"/>
<button type="submit">Edit</button>
</td>
}
</tr>
</table>
}
</td>
</tr>
</table>
上下文方法: public virtual void Update(T obj) { IAuditable audit = obj as IAuditable; IOverTime超时= obj为IOverTime;
// Existing entity
D2Repository.Updated(ref audit, UserName);
D2Repository.FromDate(ref overtime);
Set.Attach(obj);
Ctxt.Entry(obj).State = EntityState.Modified;
}
答案 0 :(得分:1)
我添加了
obj.Party.PartyId = obj.PartyId;
在我的编辑操作中,现在正在运行。 我还想知道这是否是正确的做法?