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

  • Home
  • SEARCH
  • 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 6032627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:21:43+00:00 2026-05-23T05:21:43+00:00

I create few entities in Entity Framework . They are automatically added. Question 1:

  • 0

I create few entities in Entity Framework. They are automatically added.

Question 1: Is it normal that MyEntity.CreateMyEntity automatically adds entity to the context?

Now, I want to save them but if they violate the database constraints I get exception, nothing is persisted — RIGHT, BUT — these objects remain in the ObjectContext which I do not want.

I tried to run that code as:

using (var t = new TransactionScope()) {
   ...create entities...
   try
   {
     context.SaveChanges();
     t.Complete();
     context.AcceptAllChanges();
   }
   catch
   {
      MessageBox.Show("Ooops");
   }
}

but even if I ran it in the transaction, these entities remain in the context.

Question 2: What am I missing? Do I have to remove them manually?

  • 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-23T05:21:44+00:00Added an answer on May 23, 2026 at 5:21 am

    Another option to avoid unwanted objects without having to dispose the context is to manually detach them after you have committed the changes to the DB using Detach():

    You can detach objects from an object
    context when they are no longer
    required. To do this, call the Detach
    method. This reduces the amount of
    memory used.

    context.Detach(yourEntity);
    

    Be careful with related entities though, if your entity has related objects the related entities will not be detached automatically.

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

Sidebar

Related Questions

I have few question in this regard When you create an internet page, does
I'd like to create an attribute-based validator that goes a few steps beyond what
I want to create a wizard that includes a few steps, that in the
I'm using Entity Framework 4 and I have created a UnitOfWork class that creates
I am trying to create WCF Data service project using Entity Framework. ( I
In my application, I've got a few different types of entities that I have
There're a few entities that we wish to hide its exact ID from the
I wrote a few assumptions regarding Entity Framework, then a few questions (so please
I'm trying to learn JavaFX and maybe create a few learner games. I always
I created few user defined types in my database as below CREATE TYPE [dbo].[StringID]

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.