0

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.

enter image description here

The way it is related is the following:

enter image description here

enter image description here

I don't know if I'm saving the object incorrectly or my be data base design is wrong.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
luciano cba
  • 185
  • 2
  • 13
  • `ntext`, `text`, and `image` data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use `nvarchar(max)`, `varchar(max)`, and `varbinary(max)` instead. [See details here](http://msdn.microsoft.com/en-us/library/ms187993.aspx) – marc_s Apr 13 '17 at 05:08
  • Have you seen this? http://stackoverflow.com/questions/24605152/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c looks similar to your issue. – Stuart Apr 13 '17 at 07:10
  • I did not know those fields were about to be removed, and thanks for the tips. The url help me a lot, yes it´s a similar to my issue. thanks – luciano cba Apr 13 '17 at 12:07

1 Answers1

0

I could solve the problem. In the chield Anannesis, it had the PK as identity auto-generated, how they say in link A dependent property in a ReferentialConstraint is mapped to a store-generated column error on 1-to-1 relationship, the problem is when entity framework try to save the chield and put an Id.

My unique solution was take out the PK as identity.

Community
  • 1
  • 1
luciano cba
  • 185
  • 2
  • 13