[英]Error trying to save child object with Entity Framework in a 1:1 relation (c#)
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. 我在SQL Server 2016中使用Visual Studio 2017和Entity Framework 6,尝试保存子对象时遇到问题。
[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
: 第一个对象
o_new_pac
,保存正确,但是尝试保存o_new_pac.Anannesis
时出现此错误:
InnerException: A dependent property in a ReferentialConstraint is mapped to a store-generated column.
InnerException:ReferentialConstraint中的从属属性映射到商店生成的列。 Column: 'Id'."
列:“ Id”。”
System.Exception> {System.InvalidOperationException}
System.Exception> {System.InvalidOperationException}
Between these two objects I have a 1:1 relation. 在这两个对象之间,我有1:1的关系。
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. 我不知道我是否错误地保存了对象,或者我的数据库设计错误。
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.
在生成的Anannesis中,它具有自动生成的PK作为标识,他们如何在链接中说ReferentialConstraint中的从属属性映射到存储生成的一对一关系列错误 ,问题是当实体框架尝试时保存收益并输入ID。
My unique solution was take out the PK as identity. 我独特的解决方案是删除PK作为标识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.