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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:59:50+00:00 2026-05-22T22:59:50+00:00

I have something like this: public void Delete(T entity) { Context.DeleteObject(entity); Context.SaveChanges(); } I

  • 0

I have something like this:

public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }

I end up wit a exception:
“The object cannot be deleted because it was not found in the ObjectStateManager.”
If I try to add the entity to objectContext with AttachTo() I get:

“An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.”

Whats wrong?

Example:

namespace CAFM.Data.Repository
{
    public abstract class RepositoryBase<T> : IRepository<T>
        where T : EntityObject
    {
        public RepositoryBase()
            : this(new CAFMRepositoryContext())
        {
        }
        static RepositoryBase()
        {
        }
        public RepositoryBase(IRepositoryContext repositoryContext)
        {
            _context = repositoryContext ?? new CAFMRepositoryContext();
            _entity = _repositoryContext.ObjectContext.CreateObjectSet<T>();
        }
        private readonly ObjectContext _context;
        private readonly ObjectSet<T> _entity;
        protected ObjectContext Context
        {
            get { return _context; }
        }
        protected IObjectSet<T> Entity
        {
            get { return _entity; }
        }



        #region IRepository Members

        private string GetEntityName()
        {
            return string.Format("{0}.{1}", _entity.EntitySet.EntityContainer, _entity.EntitySet.Name);
        }
        public T Add(T entity)
        {
            var fqen = GetEntityName();
            Context.AddObject(fqen, entity);
            Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            return entity;
        }
        public T Update(T entity)
        {
            Context.ApplyCurrentValues(GetEntityName(), entity);
            Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            return entity;
        }
        public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }
        #endregion
    }
}
  • 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-22T22:59:51+00:00Added an answer on May 22, 2026 at 10:59 pm

    You have to fetch the entity you wish to delete from your context first. Best to do this with a comparison of the primary key. It could look like this, but i do not know the object structure of TabMaster and TabMasterViewModel, so the properties may be wrong named.

    public void Delete(TabMasterViewModel entity) { 
        TabMaster des = _tabmasterRepository.FirstOrDefault( e.Id = entity.ID );
        if (des != null) {
            _tabmasterRepository.Delete(des); 
        }
    } 
    

    You have created a new Entity and mapped the values from your view model to that entity. But the context does not know of the entity, so he could not delete it.

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

Sidebar

Related Questions

In a bunch o' places in my code, I have something like this: public
I have a class which looks something like this: public class Test { private
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
In C#, if you have multiple constructors, you can do something like this: public
So what I have right now is something like this: PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);
I have a standalone enum type defined, something like this: package my.pkg.types; public enum
I have three database operations like so: public void Add<T>(T entity) { using (var
I have Servlet that looks something like this: public class MyServlet extends Servlet {
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <node TEXT= txt A /> <node TEXT= txt X

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.