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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:01:01+00:00 2026-06-06T09:01:01+00:00

Bottom Line Up Front Is there a succinct way that I can constrain the

  • 0

Bottom Line Up Front

Is there a succinct way that I can constrain the number of child entities that can belong to a parent in Entity Framework. I am using 4.3.1 at the moment.

The Problem

I am developing an ASP.NET MVC3 site which accesses data via a data access layer that uses Entity Framework. I have a SearchList entity which has a many to many relationship to a Search entity. A SearchList may have many Searches, and a Search may belong to many SearchLists.

At one point in the workflow of the site, a user needs to select the searches and other items to use in a batch search. We want the page to load the entire search list.

SearchLists can get quite large, and as a test we created one with 21,000 searches. It took a few seconds, and the data returned was about 9.5 MB, which we were expecting, but jQueryUI choked when trying to table-ify that much.

What we would like

So we want to impose a limit on the number of searches any search list can have. I can go through the application and put a bunch of rules in that checks the size of the collection and if the searches that are trying to be added plus the size of the current… yada yada yada.

If however there was a better way (especially one that could easily output an error message that MVC would pick up) I would totally take that instead.

I have googled, and perused a number of EF blogs to no avail. Constrain children and max number of children in collection and similar searches have returned results that are about Linq queries and the Count and Max methods.

Any help would be appreciated.

  • 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-06T09:01:03+00:00Added an answer on June 6, 2026 at 9:01 am

    I ended up going with CustomValidationAttribute, and implemented it with a great deal of success. See below for my implementation info:

    In the SearchList entity

        [NotMapped]
        public String ValidationMessage { get; set; }
        [CustomValidation(typeof(EntityValidation.EntityValidators), "ValidateSearchCount")]
        public virtual List<Search> Searches { get; set; }
    
        public static bool Create(ProjectContext db, SearchList searchList)
        {
            try
            {
                db.SearchLists.Add(searchList);
                db.SaveChanges();
                return true;
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        searchList.ValidationMessage += validationError.ErrorMessage;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    

    EntityValidators Class

        public static ValidationResult ValidateSearchCount(List<Search> Searches)
        {
            bool isValid;
            int count = Searches.Count();
            isValid = (count <= 5000 ? true : false);
    
            if (isValid)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult("A maximum of 5000 searches may be added to a SearchList.");
            }
        }
    

    A similar exception block is on the update method. In this way, when SaveChanges gets called it attempts to validate the entity and its child collections, and when the collection count is greater than 5000 the validator will return an error message which gets caught in the exception handler and stored in a local property for my controller to check when things go wrong.

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

Sidebar

Related Questions

You can skip to the bottom line if you don't care about the background:
Problem 1: There is a white space at my bottom but i can't figure
Bottom line: I have a Sub that should be re-ordering a group of records,
There is missing bottom border line gap after <dt> what is causing this and
Bottom Line Up Front I'm looking for a method to validate powershell (v1) command
I would like to add a data table to bottom of a (line) chart
Most popular browsers, while rendering an image, they display it line-by-line top-to-bottom as it
Update: See the bottom of this question for a C# workaround. Hi there, Consider
I am building CSS hover menu where sub level ul parent has set line-height
Since there's a lot of code, I won't post it here. Rather, you can

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.