I am trying to inset data from an ASP.Net Web API application into a database using Entity Framework. But I get an exception of:
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code.
the exception happens when that method fire db.SaveChanges(); Here is the code of the post method:
public HttpResponseMessage PostTransaction(Transaction transaction)
{
if (ModelState.IsValid)
{
// db.Entry(transaction).State = EntityState.Added;
db.Transactions.Add(transaction);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, transaction);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = transaction }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Here is the model:
public partial class Transaction
{
public int TrnID { get; set; }
public string TrnSubject { get; set; }
public int TrnCategoryID { get; set; }
public DateTime TrnDate { get; set; }
public int TrnAccount { get; set; }
public bool TrnType { get; set; }
public decimal TrnAmount { get; set; }
}