Entity Framework Code-First entity update is strange.

asp.net-mvc-3 c# ef-code-first entity-framework

Question

I'm trying to fix the issue with changing the object before saving to the database and have seen some odd behavior.

In my ASP.NET MVC 3 web application, Entity Framework 4.1 Code-First is being used. Here is a sample:

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; }
}

Every vehicle has a rent price. When generating an Order, the RentPrice should be duplicated from this one. Since the user chooses the vehicle, Order.RentPrice is initially set to 0.

I want to replicate the pricing value here:

[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 functioning as a result of a mistake on theSaveChanges Validation issues with that object exist. OK. I discovered a first-call requirement.UpdateModel(order); and after that alter values.

what I have, then Code in use:

_context.Orders.Add(order);
UpdateModel(order);
order.RentPrice = 777;
_context.SaveChanges();

Working code: Not

_context.Orders.Add(order);
UpdateModel(order);
order.RentPrice = _context.Cars.Find(order.CarId).RentPrice;
_context.SaveChanges();

Activated code (!)

_context.Orders.Add(order);
UpdateModel(order);
var t = (double)_context.Cars.Find(order.CarId).RentPrice;
order.RentPrice = (decimal)t;
_context.SaveChanges();

Please someone clarify what is happening. especially on the third and fourth lines of the last block of code, which read magic.

Update

I'm acquiringDbEntityValidationException : "One or more entities were unable to be validated. For further information, see the 'EntityValidationErrors' field." OriginalValues cannot be utilized for entities in the Added state, according to the inner exception.

1
6
8/18/2011 7:15:58 PM

Popular Answer

Once you do

"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 indicates that there were issues with entity validation, such as blank NOT NULL columns or other restrictions, which may be checked through debugging.

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 );
     }
    }
}
18
11/20/2012 1:39:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow