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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:01:10+00:00 2026-05-14T15:01:10+00:00

What is the best practice with regard to the Entity Framework and validating new

  • 0

What is the best practice with regard to the Entity Framework and validating new objects prior to persisting them (or in other words calling .SaveChanges()) when the new object may rely on either persisted objects or other new objects?

For example, consider the following:

Entity1 MyEntity1 = new Entity1();
MyEntity1.Name = "Hornblower";
DataContext.Entity1s.Add(MyEntity1);

.... Other code ....

Entity2 MyEntity2 = new Entity2();
MyEntity2.Entity1s.Add(MyEntity1);

.... Other code ....

// Validate that MyEntity2 has at least 1 Entity1 relationship
if (MyEntity2.Entity1s.Count() > 0 )
{
    // Valid - save it
    DataContext.SaveChanges();
} else {
    // Invalid - handle it
}

In the above pseudo code, would this be a valid and correct method of validating the required conditions – can the .Count() be relied upon to return both persisted MyEntity1s and non-persisted MyEntity1s, and thus in the above case cause the validation to succeed?

Or should I be persisting MyEntity1 prior to attaching it to MyEntity2?

Regards

Moo

  • 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-14T15:01:11+00:00Added an answer on May 14, 2026 at 3:01 pm

    Your code is a bit dis-jointed.

    You have attached MyEntity1 to the ObjectContext by calling

    DataContext.Entity1s.Add(MyEntity1);
    

    but have not attached (or have not shown in the example) MyEntity2 to the Context.

    That being besides the point, the short answer is Yes. Your validation will hold true and will pass as being valid.

    Reasoning:

    Entity manipulation is independent of Object Context. When adding or removing associations from entities, changes are reflected on the entities regardless of its state in relation to the Object Context that manages it.

    Pitfall

    Your real problem will be when calling SavingChanges() to the Context. When trying to persist entities (namely entity graphs), you must be aware that the context is very sensitive to Object States. This means you cannot persist an entity graph of mixed attached and detached entities.

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

Sidebar

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.