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

The Archive Base Latest Questions

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

I use entity framework and have a set of users: public class DbModel :

  • 0

I use entity framework and have a set of users:

 public class DbModel : DbContext
 {
     public DbSet<User> Users { get; set; }

I add a User like so:

  User UserOne = new User();
  model.Users.Add( UserOne );

I request the users count:
int userCount = model.Users.Count();

userCount is “0” I would expect “1”. Adding DetectChanges doen’t help.
After “model.SaveChanges()” the Count = 1, but that is to late I need to combine the in memory stuff with the DB stuff for validation. Is there a way to do this?

SOLUTION

Using the answer of Erik Philips I wrote the following extension method for the DbSet

public static class DBSetExtentions
{
     public static IEnumerable<T> AllMembers<T>(
         this DbSet<T> target, 
         Func<T, bool> selection
         )  where T : class
    {
        return target.Local.Where(selection).Union(target.Where(selection));
    }
 }

it allows me to do selections an validations accross all entities like:

    private void ValidateEmail(ValidationDto validationDto)
    {

        int usersWithSameEmail =
            validationDto.Model.Users.AllMembers(
                x => x.EmailAddress.Equals( EmailAddress ) ).Count();

        if (usersWithSameEmail > 1)
        {
            validationDto.Result.Add(new ValidationResult("Email address is in use"));
        }
    }
  • 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-13T15:45:05+00:00Added an answer on June 13, 2026 at 3:45 pm

    You can query the client side of items (committed and uncomitted) to your data storage by using Local.

    var count = model.Users.Local.Count();
    

    One caveat is that this is only the local representation of Users. Meaning it could contain a partial amount of users from the database (changed and/or unchanged), and new users you’ve created and not saved.

    Interesting Article – Using DbContext in EF 4.1 Part 7: Local Data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entity: public class User { public int ID {get; set;}
I have the following entities set up in Code First Entity Framework: public class
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I use Entity framework for creating model. I have table hierarchy where User is
I use Entity Framework 4 and I have parent - child relation with Cascade
I use ADO.NET Entity-Framework, I have a page that is data-bound to an entity
I'm using Entity Framework and I have a custum IQueryProvider. I use the Execute
I have converted an Entity framework project to use POCO objects by removing the
i have some questions of how to use the Entity Framework in an enterprise
The goal: I'm trying to use the new Entity Framework 4.1 DbContext API (using

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.