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 8695569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:04:17+00:00 2026-06-13T01:04:17+00:00

I have just started a new project and having had previous happy times with

  • 0

I have just started a new project and having had previous happy times with Entity Framework, I Nuget’ed in the latest version. All was going well until I tried to update an entity. I have the following code;

using (var c = new EFContext())
{
      Data.User u = new Data.User() { UserId = UserId };
      c.Users.Attach(u);
      u.FailedPasswordAttemptCount = newFailedPasswordAttemptCount;
      c.SaveChanges();
}

This worked fine in EF4. However in EF5 this craps out with a DbEntityValidationException as I have a non-nullable field in the user table (UserName).

I could solve this by creating my Data.User object with an empty UserName and telling EF which properties have been modified,

using (var c = new EFContext())
{
      Data.User u = new Data.User() { UserId = UserId, UserName = "" };
      ...
      c.Entry(u).Property("FailedPasswordAttemptCount ").IsModified = true;
      ...
}

but this doesn’t seem particularly manageable as more non-nullable fields are added to this and numerous other tables.

This question states that Attach will only work for entities derived from EntityObject. So I assume EF5 objects do not derive from this.

Anyone know how I can update my entity without having to specify the all non-nullable string fields each time.

Feels like I am missing something really obvious….

  • 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-06-13T01:04:18+00:00Added an answer on June 13, 2026 at 1:04 am

    If this is an existing user, you could just do this…

    c.Users.First(user=>user.UserId == UserId).FailedPasswordAttemptCount =         newFailedPasswordAttemptCount;
    
    c.SaveChanges(); 
    

    By the way, as an aside, in EF5 code first, entity classes don’t derive from anything, but can still be attached.

    Or, you go back to your way of attaching, but I think you need to turn off validation for this save, as it seems EF only validates the whole entity or nothing

    using (var c = new EFContext()) 
    {       
        c.Configuration.ValidateOnSaveEnabled = false; 
        Data.User u = new Data.User() { UserId = UserId };       
        c.Users.Attach(u);       
        u.FailedPasswordAttemptCount = newFailedPasswordAttemptCount;       
        c.SaveChanges(); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started a new project, and I am using the Delphi 2009
I have just started a new PHP project and I was thinking it's time
I have just started a new database project to price customer bid proposals: Some
I just started a new project in the latest Netbeans, and I noticed it
I just recently started a new personal project, with a goal of having it
I have just started working with a new Coldfusion project, well the project is
Just started a new job, working with the zend framework, the project is essentially
I have just started learning Jquery and am new to writing javascript (I am
I am new to NHibernate and have just started right now. I have very
we have just started using a git account of our Django website project so

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.