开发者

Error while Detaching Entities in Entity Framework v1

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  [Ap开发者_如何学JAVApUser](
    [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.


Why do you want to detach all entities?

Maybe you can try to use the Context.MergeOption property to retrieve a list of detached entities directly so you don't need to detach them.

See: http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx

Davide


I hesitate to ask why you're doing this, but:

When you Detach an entity, the entire graph of related entities is detached. So later in your loop you go to Detach one of those related entities and it's already been detached implicitly by an earlier iteration.


My guess, and it is just a guess, is that deleted items are detached too? It might be worth testing.

Could you change your code so you ignore deleted items?

i.e:

foreach (var stateEntry in context.ObjectStateManager.GetObjectStateEntries(
     EntityState.Added | EntityState.Modified | EntityState.Unchanged
)) {                    
   if (!stateEntry.IsRelationship)        
       context.Detach(stateEntry.Entity);    
}

Alex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜