Below is my Model class, my controller action and my view.
When I edit from my view I have got the exception:
A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
Code:
public partial class Organization : ILockable, IAuditable, IEntity
{
/*** Constructor(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; }
}
Controller Edit Action:
[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();
}
}
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>
Context method:
public virtual void Update(T obj)
{
IAuditable audit = obj as IAuditable;
IOverTime overtime = obj as IOverTime;
// Existing entity
D2Repository.Updated(ref audit, UserName);
D2Repository.FromDate(ref overtime);
Set.Attach(obj);
Ctxt.Entry(obj).State = EntityState.Modified;
}
I have added
obj.Party.PartyId = obj.PartyId;
in my edit action and it is working now. I still want to know if this is the correct way of doing it?