请看下面我的模型类、我的控制器动作和我的视图。当我从我的视图中编辑时,我遇到了异常:发生了引用完整性约束冲突:定义引用约束的属性值在关系中的主体对象和依赖对象之间不一致。
我已经问过这个问题,但我没有得到答案;请帮忙!!
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;
}