J
JaamieT
Guest
Hi MSDN,
I'm having an issue with Entity Framework and SQL Server. When I try to add a record in to a table, I get a "System.Data.Entity.Infrastructure.DbUpdateConcurrencyException" error. This is in a development environment so no changes should've been made. I can successfully retrieve data from the table and also an INSERT Sql statement using the context, but no 'Add'. The data is being parsed and stored correctly from the front end to the object.
My current controller code:
[HttpPost]
public ActionResult AddFeedback()
{
using (var context = new RiskAssessmentContext.FeedbackContext())
{
Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();
RiskAssessmentContext.RiskFeedback feedbackObject = JsonConvert.DeserializeObject<RiskAssessmentContext.RiskFeedback>(input);
feedbackObject.RiskAssessmentFeedbackID = Crypto.GenerateRandomIntID();
context.Feedbacks.Add(feedbackObject);
context.SaveChanges();
//This SQL statement successfuly inserts in to table
//context.Database.ExecuteSqlCommand("INSERT INTO dbo.tblWRAPRiskAssessmentFeedback VALUES('1231241', '320', '120', 'Test', 10/10/10, 10/10/10, '', 10/10/10, DEFAULT)");
return Json("Success");
}
}
And my Model + context :
[Table("tblWRAPRiskAssessmentFeedback")]
public class RiskFeedback
{
[Key]
public int RiskAssessmentFeedbackID { get; set; }
public int VesselNumber { get; set; }
public int RiskAssessmentID { get; set; }
public string Feedback { get; set; }
public DateTime? ReadByOffice { get; set; }
public DateTime? ReadByVessel { get; set; }
public string CreatedBy { get; set; }
public DateTime? Created { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public byte[] Upsize_ts { get; set; }
}
public class FeedbackContext : DbContext
{
public FeedbackContext(): base("DatabaseConnection")
{
Database.Log = s => Debug.WriteLine(s);
}
public DbSet<RiskFeedback> Feedbacks { get; set; }
}
And the current setup of the SQL Table
RiskAssessmentFeedbackID - int - Not Null
VesselNumber - int - Not Null
RiskAssessmentID - int - Nullable
Feedback - ntext - Nullable
ReadByOffice - datetime2(7) - Nullable
ReadByVessel- datetime2(7) - Nullable
CreatedBy - nvarchar(30) - Nullable
Created - datetime2(7) - Nullable
upsize_ts - timestamp - Nullable
(This is my first post to unable to include screenshot :c )
Please let me know if there's any more detail that could be useful!
Any ideas or advice would be massively appreciated, thanks in advance!
Jamie
Continue reading...
I'm having an issue with Entity Framework and SQL Server. When I try to add a record in to a table, I get a "System.Data.Entity.Infrastructure.DbUpdateConcurrencyException" error. This is in a development environment so no changes should've been made. I can successfully retrieve data from the table and also an INSERT Sql statement using the context, but no 'Add'. The data is being parsed and stored correctly from the front end to the object.
My current controller code:
[HttpPost]
public ActionResult AddFeedback()
{
using (var context = new RiskAssessmentContext.FeedbackContext())
{
Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();
RiskAssessmentContext.RiskFeedback feedbackObject = JsonConvert.DeserializeObject<RiskAssessmentContext.RiskFeedback>(input);
feedbackObject.RiskAssessmentFeedbackID = Crypto.GenerateRandomIntID();
context.Feedbacks.Add(feedbackObject);
context.SaveChanges();
//This SQL statement successfuly inserts in to table
//context.Database.ExecuteSqlCommand("INSERT INTO dbo.tblWRAPRiskAssessmentFeedback VALUES('1231241', '320', '120', 'Test', 10/10/10, 10/10/10, '', 10/10/10, DEFAULT)");
return Json("Success");
}
}
And my Model + context :
[Table("tblWRAPRiskAssessmentFeedback")]
public class RiskFeedback
{
[Key]
public int RiskAssessmentFeedbackID { get; set; }
public int VesselNumber { get; set; }
public int RiskAssessmentID { get; set; }
public string Feedback { get; set; }
public DateTime? ReadByOffice { get; set; }
public DateTime? ReadByVessel { get; set; }
public string CreatedBy { get; set; }
public DateTime? Created { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public byte[] Upsize_ts { get; set; }
}
public class FeedbackContext : DbContext
{
public FeedbackContext(): base("DatabaseConnection")
{
Database.Log = s => Debug.WriteLine(s);
}
public DbSet<RiskFeedback> Feedbacks { get; set; }
}
And the current setup of the SQL Table
RiskAssessmentFeedbackID - int - Not Null
VesselNumber - int - Not Null
RiskAssessmentID - int - Nullable
Feedback - ntext - Nullable
ReadByOffice - datetime2(7) - Nullable
ReadByVessel- datetime2(7) - Nullable
CreatedBy - nvarchar(30) - Nullable
Created - datetime2(7) - Nullable
upsize_ts - timestamp - Nullable
(This is my first post to unable to include screenshot :c )
Please let me know if there's any more detail that could be useful!
Any ideas or advice would be massively appreciated, thanks in advance!
Jamie
Continue reading...