If I have, for example, a person class 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 Save changes , it saves the user and the address in cascade .
That's fine and that's how it should be, now, this person also has a profile, I set a profile for the person , but when saving I don't want it to generate a record in the profiles table , but only save it in the personsidperfil
table , i.e. don't cascade save .
How can I set the person class to not cascade save ?
There is something I don't understand, here is my code 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 cause of death table I have the idUsuario
person who charged the reason of death in the system. What happens is that when saving the reason of death, it saves a user tuple in the users table, but it should not, it should only save the reason of deathidUsuario
in the table so that the relationship remains.
You should analyze the assignment
State
of that objectEntity Framework Add and Attach and Entity States
if it is a disconnected object you will use the
Attach()
and with this theState
In this way, 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.