Hello,
I'm using .NET 3.5 SP1. Using VS2008 Designer, I created entity 'Category' based on table 'Category' and 'AppUser' based on table 'AppUser' and 'AppUserDetail' based on table 'AppUserDetail'.
DB TABLES:
CREATE TABLE [Category](
[CategoryId] [int] NOT NULL,
[CategoryName] [varchar](50) NOT NULL,
PRIMARY KEY ([CategoryId])
)
CREATE TABLE [AppUser](
[UserId] [int] NOT NULL,
[UserName] [varchar](50) NOT NULL,
[CategoryId] [int] NOT NULL,
PRIMARY KEY ([UserId]),
FOREIGN KEY (CategoryId) REFERENCES Category(CategoryId) ON DELETE CASCADE
)
CREATE TABLE AppUserDetail (
DetailId int NOT NULL,
UserId int not null,
Address varchar(2000) not null,
Comments varchar(2000) not null,
PRIMARY KEY ([DetailId] ),
FOREIGN KEY (UserId) REFERENCES AppUser(UserId) ON DELETE CASCADE
)
TABLE RECORDS:
Category: 1, Category-1
AppUser: 1, User1, 1
AppUserDetail: 1, 1, Address-1, Comments-1
Using following code,I retrieve a user and then try to detach all entities in context.
using (var context = new MyEntities()) {
AppUser user = context.AppUserSet.Where(u => u.UserId == 1).FirstOrDefault();
//Detach ALL entities
foreach (var stateEntry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)) {
if (!stateEntry.IsRelationship)
context.Detach(stateEntry.Entity);
}
}
I'm getting the following exception:
"System.InvalidOperationException was unhandled
Message="The object is in a detached state. This operation cannot be performed on an ObjectStateEntry when the object is detached."
Source="System.Data.Entity"
StackTrace:
at System.Data.Objects.ObjectStateEntry.get_IsRelationship()
In code I'm only selecting entities which are not Detached.
Please tell what is cause of this error ?
Thank you.