locked
EF 4.0 RRS feed

  • Question

  • User1122570667 posted

    Hi

    I'm working on EF 4.0. I suppose to do select a row from table X and after some changes the same row has to insert in the same table X with the new value. Please suggest is there any workaround for this.

    Wednesday, November 3, 2010 1:42 AM

Answers

  • User1014708013 posted

    You're trying to detach an entity from one context that was created by another context.

    GetActiveCompanyDetailByCompanyId uses Scope as the context and then returns it. This means that the entity is tracked by the Scope context, not by your obj context. This in turn means there's nothing to detach from obj and thus you get this error.

    You need to use the same context, or you could try detaching the object in the GetActiveCompanyDetailByCompanyId method before you return it.

    Hope this helps,

    Imar

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 9:08 AM

All replies

  • User-443671128 posted

    You can use below stuff...

    first retrieve the object using EF 4.0
    change the value
    update the object by using SaveChanges() method

    the only thing you need to take care is context should be the same in all above 3 logic as the EF store changes in context and do update as per the same. 

    Wednesday, November 3, 2010 3:14 AM
  • User1014708013 posted

     Hi there,

    You can query the object, create a new instance of the same type, copy over the relevant values, attach the new object and then save the changes back. Something like this may work:

    using (MyEntities context = new MyEntities())
    {
      Category original = context.Categories.Where(c => c.Id == 1).First();
      Category copy = new Category();
      copy.Description = original.Description;
      context.AddToCategories(copy);
      context.SaveChanges();
    }
    

    This code gets a (random) Category based on its ID, then creates a new one and copies over a single property (Description), then adds the new categioy to the context and saves the changes.

    Hope this helps,

    Imar

     

    Wednesday, November 3, 2010 3:18 AM
  • User1122570667 posted

    hi Imar thanx for reply.

      # using (MyEntities context = new MyEntities())  
      # {  
      #   Category original = context.Categories.Where(c => c.Id == 1).First();  
      #   original.Id = 0
      #   context.AddToCategories(original);  
      #   context.SaveChanges();  
      # } 
      
      or
      
      # using (MyEntities context = new MyEntities())  
      # {  
      #   Category original = context.Categories.Where(c => c.Id == 1).First();  
      #   context.AddToCategories(original);  
      #   context.SaveChanges();  
      # } 


    I want to do some thing like above.. is this possible in EF. actually i want to resemble( SQL : Insert Category(a,b) Select a,b from Category)

    Wednesday, November 3, 2010 5:54 AM
  • User1014708013 posted

     You could detach the original, reset its ID and then add it again, like this:

    using (MyEntities context = new MyEntities())
    {
      Category original = context.Categories.Where(c => c.Id == 1).First();
      context.Detach(original);
      original.Id = 0;
      context.AddToCategories(original);
      context.SaveChanges();
    }
    


    This works fine for simple entities; haven't tested what happens when you do this to an object graph.

    Cheers,

    Imar

    Wednesday, November 3, 2010 6:24 AM
  • User1122570667 posted

    hi imar thanx again for ur reply.

    now i've got this error : The object cannot be detached because it is not attached to the ObjectStateManager.

    thanx....

    Wednesday, November 3, 2010 7:08 AM
  • User1014708013 posted

    Are you querying the object from the context as in my example? Can you show us your code?

    Imar

    Wednesday, November 3, 2010 7:11 AM
  • User1122570667 posted
    i've done similar code as u stated in eg: context obj = new context(); category obj = (from s in category select s).First(); obj.Detach(category); I'm getting error while detaching the object from context.
    Wednesday, November 3, 2010 7:59 AM
  • User1014708013 posted

    Can you show the real code you're using? it's a bit difficult to look at an issue based on non-working code....

    Imar 

    Wednesday, November 3, 2010 8:06 AM
  • User1122570667 posted
    hi imar.. thanx again.. here is the actual code.. Entity.CompanyDetail ActiveComapnyDetails = CompanyDetailController.ActiveCompanyDetailByCompanyId(companyID); Entity.WEntities obj = new Entity.WEntities(); obj.Detach(ActiveComapnyDetails); return ActiveComapnyDetails; -- private static CompanyDetail GetActiveCompanyDetailByCompanyId(int companyId) { return (from cd in Scope.CompanyDetails where cd.CompanyID == companyId && cd.Active == true select cd).FirstOrDefault(); } hope u can get the clear picture now... thanx for spend some time on this issue...
    Wednesday, November 3, 2010 8:48 AM
  • User1122570667 posted

    hi imar.. thanx again.. here is the actual code..

    Entity.CompanyDetail ActiveComapnyDetails = CompanyDetailController.ActiveCompanyDetailByCompanyId(companyID);
    Entity.WEntities obj = new Entity.WEntities();
    obj.Detach(ActiveComapnyDetails);
    return ActiveComapnyDetails;
    
    --
    
     private static CompanyDetail GetActiveCompanyDetailByCompanyId(int companyId)
     {
                    return (from cd in Scope.CompanyDetails
                            where cd.CompanyID == companyId && cd.Active == true
                            select cd).FirstOrDefault();
     }




    hope u can get the clear picture now... thanx for spend some time on this issue...

    some problem with editory previously..


    Wednesday, November 3, 2010 8:51 AM
  • User1122570667 posted

    Entity.CompanyDetail ActiveComapnyDetails = CompanyDetailController.ActiveCompanyDetailByCompanyId(companyID);
    Entity.WEntities obj = new Entity.WEntities();
    obj.Detach(ActiveComapnyDetails);
    return ActiveComapnyDetails;
    
    --
    
     private static CompanyDetail GetActiveCompanyDetailByCompanyId(int companyId)
     {
                    return (from cd in Scope.CompanyDetails
                            where cd.CompanyID == companyId && cd.Active == true
                            select cd).FirstOrDefault();
     }



    Wednesday, November 3, 2010 8:52 AM
  • User1014708013 posted

    You're trying to detach an entity from one context that was created by another context.

    GetActiveCompanyDetailByCompanyId uses Scope as the context and then returns it. This means that the entity is tracked by the Scope context, not by your obj context. This in turn means there's nothing to detach from obj and thus you get this error.

    You need to use the same context, or you could try detaching the object in the GetActiveCompanyDetailByCompanyId method before you return it.

    Hope this helps,

    Imar

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 9:08 AM
  • User1122570667 posted

    Imar.. you are gr8.. it works gr8.. thanx for spending time..


    regards,

    Balaji


    Wednesday, November 3, 2010 10:18 AM