Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:11:33+00:00 2026-05-31T11:11:33+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:11:34+00:00Added an answer on May 31, 2026 at 11:11 am

    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);
      //...
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC 3 project that I am working on using Entity Framework
I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So
I am working on a mvc3.0 app using EF code first and mvc scaffolding.
I'm working on a wiki project. MVC3 + EF4 Code First. All classes from
I am working on project using MVC 3.0(Razor framework). I am trying to get
I am working on asp.net MVC 3 project. I am using EF 4.1 code
I'm working on a web application project using ASP.NET MVC3 and database in SQL
I'm working on an MVC3 project right now and just started using SignalR. I
I am currently working on a MVC3 project in C# and VS2010 using a
My team working in a project using asp.net mvc3(c#). Based on the project requirement,we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.