none
Attach method and the relationships RRS feed

  • Question

  • Hi all,

    I'm using the Attach method with EF in order to manage my data. But I've found some problem when I want to change the value of a field (in clients table) which is a FK with another table (rates table).

    At first, I've needed to do the first 'IF' you see in order to be able to assign the rate properly, because it seems it's necessary to define the full object if I call to the Attach method.

    But now, I see I can't set that field to null (the field in DB allows nulls). A exception is thrown that says:

    A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

    This is the code (cliente = client, tarifa = rate):

    if (cliente.TariCodi.HasValue)
                    {
                        cliente.Tarifa = new Tarifa() { TariCodi = cliente.TariCodi.Value };
                    }
    
                    //context.Attach(cliente);
                    context.Clientes.Attach(cliente);
                    context.ObjectStateManager.ChangeObjectState(cliente, System.Data.EntityState.Modified);
                    conn.SaveChanges();

    Any idea will be great! :)

    Thanks a lot,
    Carlos.

    Thursday, August 16, 2012 8:04 AM

Answers

All replies