Print Page | Close Window

Getting Error while adding entity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=892
Printed Date: 11-Jun-2026 at 1:37am


Topic: Getting Error while adding entity
Posted By: keval
Subject: Getting Error while adding entity
Date Posted: 17-Jul-2008 at 12:12am
Hi,
 
I m getting the following error while adding entity to entitymanager
 
"This Entity already belongs to another EntityManager"
 
Following is the code snippet where i get the error

DataSet.AddEntity(mmp);

- DataSet is EntityManager
- mmp is Entity
 
Can u please guide me what can be the error. AS i m not adding this entity to anywhere in the application thenalso getting the error.
 
Thanks in advance.



Replies:
Posted By: kimj
Date Posted: 17-Jul-2008 at 10:34am
Well, you'll get this error if the entity you're trying to add already has its .EntityManager property set and the entity is in a non-detached state.  How did the entity come into existence?  Did you query for it, restore it from an entity set, or create it?   If you do want to add the entity to this EntityManager, first call entity.RemoveFromManager to detach it from its current EntityManager.  Once added, the entity's state will be Added, and when SaveChanges is called it will be inserted to the database.  If you don't want this behavior, then look at using mgr.ImportEntities to "move" entities between Entity Managers.


Posted By: keval
Date Posted: 18-Jul-2008 at 12:10am
Hi Kim,
 
Thanks for the reply.
 
I have resolved the issue this morning. But its a very descriptive as well as useful reply from ur side.
That will be very useful for us to optimize the code as well as improve performance. I will try this thing in my code and check it.
 
Thanks again. 
 


Posted By: keval
Date Posted: 18-Jul-2008 at 12:11am
Hi Kim,
 
Thanks for the reply.
 
I have resolved the issue this morning. But its a very descriptive as well as useful reply from ur side.
That will be very useful for us to optimize the code as well as improve performance. I will try this thing in my code and check it eventhough i have solved it.
 
Thanks again. 
 



Print Page | Close Window