I'm new at EF in MVC. I'm needed to implement a concurrency check in the application.
I was following the MSDN help regarding built-in suport for Concurrency handling in EF: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
But in my case, I'm doing a partial update, so I'm using AJAX to call a server side method to do that. in this server side method I need to have the concurrency check implemented.
Following the tutorial I made the required changes in my code:
First in my DB I created the timestamp column:
ALTER TABLE dbo.Job
ADD RowVersion rowversion NULL;
in Entity class:
[Timestamp]
public byte[] RowVersion { get; set; }
in my Mapping class (using fluent api):
this.Property(t =>
t.RowVersion).HasColumnName("RowVersion").IsConcurrencyToken();
in my view model class:
[Timestamp]
public byte[] RowVersion { get; set; }
in my view file:
@Html.HiddenFor(x => x.RowVersion)
in JS file (the ajax call):
function UpdateJobTransferStatus(jobTransferStatusId, newJobTransferSatusId, currentAction, statusName) {
var hdnJobId = $("#JobId").val();
//var hdnRowVersion = $("#RowVersion").val();
var hdnAccountingId = $("#hdnAccountingSystemId").val();
$.ajax(
{
type: "GET",
url: ResolveUrl("~/Api/Job/UpdateJobTransferStatus"),
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { jobId: hdnJobId, currentAction: currentAction, jobTransferStatusId: newJobTransferSatusId, accountingSystemId: hdnAccountingId },//,rowVersion: hdnRowVersion },
success: function (data) {
GetPreviousOrNextStatus(jobTransferStatusId, currentAction, statusName, hdnAccountingId);
},
error: function (result) {
}
});
}
Finally in my controller (on my ajax call):
I added the namespace as per MSDN (using System.Data.Entity.Infrastructure;
)
[System.Web.Http.HttpGet]
public HttpResponseMessage UpdateJobTransferStatus(int jobId, int currentAction,
int jobTransferStatusId, short accountingSystemId, byte[] rowVersion)
Here I'm always getting 'null
' for 'byte[] rowVersion
', regardless of if I sent the value as a param in AJAX call or not (I kept in commented in my code snippet, that I pasted here).
I've checked, the column is getting updated in DB for each successful Insert/Update execution and also the view model is getting the latest value from DB for this RowVersion column on each page load.
Here in the MSDN example they have submitted the form, but I'm doing it using AJAX call, except that I tried to kept every aspect the same.
Sending the entire view model object may do the job for me, but I don't need the entire object to perform my partial update (I've not tried that though).
How can I pass the rowVersion
to the ajax call?