locked
Update Exception - Addobject RRS feed

  • Question

  • Hi,

     

    I have two tables/entities -

    say,  Table1 and Table2

    There is a FK relation b/w the 2 tables on the field ID.  ID is PK in Table 1.

     

    Table 1 -    ID    Name

                     1       abc

                     2       xyz

                     3       lmn

     

    Table 2 -   ID      Desc  

                    1       Desc11

                    1       Desc12

                    1       Desc13

                    2       Desc21

                    2       Desc22

                    3       Desc31

     

    I am trying to add a new row to Table 1 and Table 2 one after the other.

     Am using the AddObject(obj) method and then did SaveChanges().

    The database reflects the change in Table1. (say, ID - 4 was added) 

    I also did a Refresh(RefreshMode.StoreWins, obj).

    Now when I am trying to add the corresponding data for ID4 in Table2.

    The SaveChanges throws an Update exception now.

    How to resolve this?

     

    Wednesday, April 16, 2008 8:57 PM

Answers

  • Your failedEntities query does not return entities but anonymous type instances containing entities ("new { p.Entity }").Try the following instead:

     

    Code Snippet

    var failedEntities =

        from p in opt.StateEntries

        where p.Entity != null     // return only entities, not relationships

        select p.Entity;

    pe.Refresh(System.Data.Objects.RefreshMode.StoreWins, failedEntities);

     

     

    Thanks,

    -Colin

    Thursday, April 17, 2008 2:34 PM
  • You need to further filter those entities in the added state.  I think you can just add a further condition to the where clause of your linq query for EntityState != EntityState.Added.

     

    - Danny

     

    Tuesday, April 22, 2008 4:10 AM

All replies

  • Could you forward the message from the update exception?

     

    Thanks,

    -Colin

    Thursday, April 17, 2008 2:09 PM
  • I managed to sort out the Update Exception; but now am getting Invalid Operation exception

    This is what i did in the catch block for Update Exception:

     

    var failedEntities = from p in opt.StateEntries.ToList()

    select new { p.Entity };

    pe.Refresh(System.Data.Objects.RefreshMode.StoreWins, failedEntities.ToList());

     

    The 2nd line is giving me Invalid Operation exception.. with the foll msg:

    "The element at index 0 in the collection of entities to refresh has a null EntityKey property value or is not attached to this ObjectStateManager."

     

     

     

    Thursday, April 17, 2008 2:21 PM
  • Your failedEntities query does not return entities but anonymous type instances containing entities ("new { p.Entity }").Try the following instead:

     

    Code Snippet

    var failedEntities =

        from p in opt.StateEntries

        where p.Entity != null     // return only entities, not relationships

        select p.Entity;

    pe.Refresh(System.Data.Objects.RefreshMode.StoreWins, failedEntities);

     

     

    Thanks,

    -Colin

    Thursday, April 17, 2008 2:34 PM
  • Thanks...

    Am getting same exception, another msg now:

     

    "The element at index 0 in the collection of entities to refresh is in the Added state which is not meaningful to refresh."

    Thursday, April 17, 2008 3:14 PM
  • You need to further filter those entities in the added state.  I think you can just add a further condition to the where clause of your linq query for EntityState != EntityState.Added.

     

    - Danny

     

    Tuesday, April 22, 2008 4:10 AM
  • Hi Danny

    I am facing the same issue. I am not able to refresh the entity object. Here's my code sample.

    if (conn.State != ConnectionState.Open)
    
    {conn.Open();
    
    }
    //Here's my entity class
    MasterEntities masterEntities = new MasterEntities();
    
    //masterEntities .ObjectStateManager.ChangeObjectState(masterEntities .masterEntity, EntityState.Detached);          
    
     //masterEntities .Refresh(RefreshMode.StoreWins, masterEntities.masterEntity);
    
    var LastName = masterEntities.Address.Where(s => s.FirstName == "marley").First().LastName;                
    
    conn.Close();
    

    Still I am not able to refresh the data . Even if I uncomment the code shown above, it dosen't refresh the data.

     

     

    Friday, November 12, 2010 12:41 PM