I'm solving the problem with updating entity before saving to database and got strange behavior.
I'm using Entity Framework 4.1 Code-First in ASP.NET MVC 3 web application. Here is model:
public class Order
{
public int OrderId { get; set; }
public int CarId { get; set; }
public DateTime BeginRentDate { get; set; }
public DateTime EndRentDate { get; set; }
public decimal RentPrice { get; set; }
public virtual Car Car { get; set; }
}
public class Car
{
public int CarId { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public string NumberPlate { get; set; }
public decimal RentPrice { get; set; }
}
Each Car has a RentPrice. This price should be copied to Order's RentPrice when creating one. The car is selecting by user so initially Order.RentPrice is 0.
Here I want to copy price value:
[HttpPost]
public ActionResult Create(Order order)
{
order.RentPrice = _context.Cars.Find(order.CarId).RentPrice;
if (ModelState.IsValid)
{
_context.Orders.Add(order);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(order);
}
It's not working because of an error on the SaveChanges
that entity has validation errors. OK. I found that need first to call UpdateModel(order);
and then change values.
So what I have. Working code:
_context.Orders.Add(order);
UpdateModel(order);
order.RentPrice = 777;
_context.SaveChanges();
Not working code:
_context.Orders.Add(order);
UpdateModel(order);
order.RentPrice = _context.Cars.Find(order.CarId).RentPrice;
_context.SaveChanges();
Working code (!):
_context.Orders.Add(order);
UpdateModel(order);
var t = (double)_context.Cars.Find(order.CarId).RentPrice;
order.RentPrice = (decimal)t;
_context.SaveChanges();
Can someone explain, please, what is going on here? Especially magic on the 3nd and 4th lines in the last block of code.
Update
I'm getting DbEntityValidationException
: "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
From the inner exception: "OriginalValues cannot be used for entities in the Added state."
When you get
"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." From the inner exception: "OriginalValues cannot be used for entities in the Added state."
It means there was errors such as NOT NULL collumns that were blank or other constraints , check the entity validation errors by debugging or like
try{
...
catch ( DbEntityValidationException ex )
{
foreach ( var validationErrors in ex.EntityValidationErrors )
{
foreach ( var validationError in validationErrors.ValidationErrors )
{
System.Diagnostics.Trace.TraceInformation( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
}
}
}