1

Im working on a MVC3 project using code first entity framework. On a webpage a button is pushed.

Part of what happends is that a Sale-object is created to be saved in a database:

 var newSale = new Sale
                                  {
                                      Id = Guid.NewGuid(),
                                      Material = material,
                                      Buyer = buyer,
                                      CashOut = null,
                                      Token = response.Token,
                                      TimeStamp = null
                                  };

                dataContext.Add(newSale);
                dataContext.SaveChanges();

After you will be redirected to another controller function that edits the value of the TimeStamp-property of the Sale object.

        var dataContext = FOSDataContextFactory.Create();
        var = dataContext.Sales.SingleOrDefault(x => x.Token == tokenId);

        if (sale != null)
        {
            sale.TimeStamp = DateTime.UtcNow;    
            dataContext.SaveChanges();
        }

When im steping through the code using the debugger everything works fine and the TimeStamp - property is changed. But when running the web-application without debugging the code a error occurs:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

This is the error that i got:

Entity of type: Sale_9C4571E6D8D390FBA94D51E54B356016DF8C20533C767502369B99F24C117B5B in state: Modified - Property: Material, Error: The Material field is required. - Property: Buyer, Error: The Buyer field is required.

What can be the cause of this problem?

2
  • And what are the details in the EntityValidationErrors? In case you are unsure how to inspect this exception: stackoverflow.com/a/7798264/270591 Commented Mar 15, 2012 at 23:06
  • This is the error that i got: Entity of type: Sale_9C4571E6D8D390FBA94D51E54B356016DF8C20533C767502369B99F24C117B5B in state: Modified - Property: Material, Error: The Material field is required. - Property: Buyer, Error: The Buyer field is required. Commented Mar 16, 2012 at 8:38

2 Answers 2

3

The Material and Buyer properties of your Sale entity seem to be navigation properties refering to other entities. And you likely have marked these properties as virtual which means that they get lazily loaded as soon as you access them.

If you watch these properties in the debugger you trigger lazy loading when you access them through the property watch window. This does not happen when you just run the application in Release mode. Because the properties seem to be marked as required you get the exception in Release mode because your code does not access the properties and they stay null which causes the validation exception.

You have three options to fix the problem:

  • Introduce foreign key properties into your model:

    public class Sale
    {
        // ...
        public int MaterialId { get; set; }    // FK property
        public Material Material { get; set; }
    
        public int BuyerId { get; set; }       // FK property
        public Buyer Buyer { get; set; }
        // ...
    }
    

    When you load the Sale entity the FK properties get loaded as well and EF will consider the required references as set and validation won't complain.

  • Load the navigation properties with Include:

    var = dataContext.Sales
        .Include(s => s.Material)
        .Include(s => s.Buyer)
        .SingleOrDefault(x => x.Token == tokenId);
    
  • Turn off the validation, just for this operation:

    var dataContext = FOSDataContextFactory.Create();
    dataContext.Configuration.ValidateOnSaveEnabled = false;
    
    var = dataContext.Sales.SingleOrDefault(x => x.Token == tokenId);
    //...
    
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you let sql (assuming you use sql server) create the timestamp for you? You shouldn't have to set manually

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.