I'm using Visual Studio 2017 and Entity Framework 6 with SQL Server 2016, I have a problem when I try to save a child object.
[Authorize]
public ActionResult Agregar(Pacientes oPaciente)
{
if (oPaciente.Nombre != null)
{
using (var oModel = new pamidbEntities())
{
var o_new_pac = new Pacientes();
o_new_pac.Apellido = oPaciente.Apellido;
o_new_pac.Nombre = oPaciente.Nombre;
o_new_pac.DNI = oPaciente.DNI;
o_new_pac.NroAfiliado = oPaciente.NroAfiliado;
o_new_pac.Sexo = oPaciente.Sexo;
o_new_pac.Correo = oPaciente.Correo;
o_new_pac.FechaNacimiento = oPaciente.FechaNacimiento;
o_new_pac.Descripcion = oPaciente.Descripcion;
oModel.Pacientes.Add(o_new_pac);
oModel.SaveChanges();
o_new_pac.Anannesis = oPaciente.Anannesis;
oModel.Anannesis.Add(o_new_pac.Anannesis);
oModel.SaveChanges();
return RedirectToAction("Index", "Pacientes");
}
}
return View();
}
The first object o_new_pac
, it is saved ok, but I get this error when try to save o_new_pac.Anannesis
:
InnerException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'."
System.Exception> {System.InvalidOperationException}
Between these two objects I have a 1:1 relation.
The way it is related is the following:
I don't know if I'm saving the object incorrectly or my be data base design is wrong.