Do not cascade in Entity Framework

1

I have a question that I have not been able to resolve.

If I have for example a class person and this is related to an address, in a separate table, one by one, then I create a person object, I set its address object and when saving with savechanges it saves the user and the address " in cascade ".

That's fine and it should be, now, this person also has a profile, the person set a profile, but when saving I do not want to generate a record in the profiles table, but only save the idperfil in the table people, bone that does not keep "in cascade".

Is there a way to set or configure the person class so that it does not cascade? Thank you very much

--- edition --- There is something I do not understand, here I pass the code and my insert

public Boolean insert()
        {
            try
            {
                using (ModelSistema contexto = new ModelSistema())
                {
                    MotivoFallecimiento a = this.toEntity();
                    contexto.MotivosFallecimientos.Add(a);
                    contexto.SaveChanges();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }

        }

in the reason of death table I have the user ID of the person in charge of the motivation in the system. What happens is that when you save the motivofallecimiento saves a user tuple in the users table, but should not, you should only save the user id in the table groundsfallecimiento so that the relationship remains, I hope you understand a little more.

    
asked by Lucho 15.03.2017 в 23:53
source

1 answer

1

You should analyze the allocation of State of that object

Entity Framework Add and Attach and Entity States

if it is a disconnected object you will use the Attach() and with this the State

context.Entry(entity).State = EntityState.Unchanged; 

of this form if the instance only has the id because you use it to relate it will take that value without affecting the rest of the entity

    
answered by 16.03.2017 в 11:29