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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:24:33+00:00 2026-05-13T18:24:33+00:00

I have a problem with some simple code, I’m refactoring some existing code from

  • 0

I have a problem with some simple code, I’m refactoring some existing code from LINQ to SQL to the Entity Framework. I’m testing my saves and deletes, and the delete is really bugging me:

[TestMethod]
public void TestSaveDelete()
{
    ObjectFactory.Initialize(x =>
    {
        x.For<IArticleCommentRepository>().Use<ArticleCommentRepository>();
    });

    PLArticleComment plac = new PLArticleComment();
    plac.Created = DateTime.Now;
    plac.Email = "myemail";
    plac.Name = "myName";
    plac.Text = "myText";
    plac.Title = "myTitle";

    IArticleCommentRepository acrep = ObjectFactory.GetInstance<IArticleCommentRepository>();
    try
    {
        PortalLandEntities ple = new PortalLandEntities();
        int count = ple.PLArticleComment.Count();
        acrep.Save(plac);
        Assert.AreEqual(ple.PLArticleComment.Count(), count + 1);
        //PLArticleComment newPlac = ple.PLArticleComment.First(m => m.Id == plac.Id);
        //ple.Attach(newPlac);
        acrep.Delete(plac);
        Assert.AreEqual(ple.PLArticleComment.Count(), count + 1);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Every time i try to run this code, I get an exception in the delete statement, telling me that its not contained within the current ObjectStateManager.Please note that both my Save and delete looks like this:

public void Delete(PLCore.Model.PLArticleComment comment)
{
    using (PortalLandEntities ple = Connection.GetEntityConnection())
    {
        ple.DeleteObject(comment);
        ple.SaveChanges();
    }
}

public void Save(PLCore.Model.PLArticleComment comment)
{
    using (PortalLandEntities ple = Connection.GetEntityConnection())
    {
        ple.AddToPLArticleComment(comment);
        ple.SaveChanges();
    }
}

and the connection thingy:

public class Connection
{
    public static PortalLandEntities GetEntityConnection()
    {
        return new PortalLandEntities();
    }
}

Any ideas on what i could do to make it work?

  • 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-13T18:24:34+00:00Added an answer on May 13, 2026 at 6:24 pm

    You cannot load an entity from one ObjectContext (in your case, an ObjectContext is an instance of PortalLandEntities) and then delete it from another ObjectContext, unless you detach it from the first and attach it to the second. Your life will be much, much simpler if you use only one ObjectContext at a time. If you cannot do that, you must manually Detach and then Attach first, all the while keeping track of which entities are connected to which ObjectContext.

    How to use DI with your Connection : make it non-static.

    public class Connection
    {
        private PortalLandEntities _entities;
    
        public PortalLandEntities GetEntityConnection()
        {
            return _entities;
        }
    
        public Connection(PortalLandEntities entities)
        {
            this._entities = entities;
        }
    }
    

    Then use a DI container per request. Most people do this via a controller factory.

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

Sidebar

Related Questions

I have a problem with some simple code. The photo's, either picked or taken
I have some problem's with a simple application in JSF 2.0. I try to
For a few days a have some problem. I need to show simple ProgressBar
I'm working on a simple board game in qt, and i have some problem
I have a simple problem that I've been stuck on for some time and
I have some problem to get the utf-8 string from PHP using jQuery $.load()
I have some problem in my JDBC code. I am trying to connect through
Here is a simple code I have problem with: <label id=label1> <?php echo Text
I have an IBAction with some simple code inside: -(IBAction)change:(id)sender { [textfield setHidden:NO]; [self
I have some simple test code which I am trying to generate AVX optimized

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.