简体   繁体   English

EF-与孩子一起保存父实体

[英]EF - Saving Parent Entity with Child

I have 2 entities.....with a 1-0 relation (Documento and Archivo) 我有2个实体.....具有1-0关系(Documento和Archivo)

public class Documento
{
    public int DocumentoID { get; set; }
    public int? ExpedienteID { get; set; }
    public int? TipoDocumentoID { get; set; }
    public int? ArchivoID { get; set; }
    public DateTime? FechaRegistro { get; set; }
    public int? Fojas { get; set; }
    public string Referencia { get; set; }
    public bool TieneArchivoFisico { get; set; }
    public DocumentoAdicional? Adicional { get; set; }
    public virtual Expediente Expediente { get; set; }
    public virtual TipoDocumento TipoDocumento { get; set; }
    public virtual Archivo Archivo { get; set; }

}

    public class Archivo
{
    public int ArchivoID { get; set; }
    public int DocumentoID { get; set; }
    public string Nombre { get; set; }
    public string Descripcion { get; set; }
    public string Resumen { get; set; }
    public byte[] DatosArchivo{ get; set; }

    public virtual Documento Documento { get; set; }

}

When I save this 2...they save correctly to the DB, but they are left unconnected.... This is the code I am using to save them: 当我保存此2 ...时,它们已正确保存到DB,但是它们保持未连接状态。...这是我用来保存它们的代码:

public bool AdjuntarDocumentoAExpediente(Documento documento, string nombreArchivo, Stream streamArchivo, string descripcion, string resumen, out string mensajeError)
    {
        bool resultado = false;
        mensajeError = string.Empty;
        try
        {
            using (var contexto = new ModeloTC())
            {
                documento.TieneArchivoFisico = true;

                Archivo archivo = new Archivo();
                archivo.Documento = documento; //adding relation here

                archivo.Descripcion = descripcion;
                archivo.Nombre = nombreArchivo;
                archivo.Resumen = resumen;
                archivo.DatosArchivo = ConvertirStreamABytes(streamArchivo);

                contexto.Archivos.Add(archivo);
                contexto.SaveChanges();
                resultado = true;
            }
        }
        catch (Exception ex)
        {
            mensajeError = "BLL AdjuntarDocumentoAExpediente:" + ex.GetBaseException().Message;
        }
        return resultado;
    }

The problem is...that after executing this method, this is how my DB data looks like.... 问题是...执行此方法后,这就是我的数据库数据的样子...。

Table Archivo
ArchivoID   DocumentoID Nombre  Descripcion Resumen DatosArchivo
5   0   C:\Users\test\justafilename.txt lalala  lalalalala  

Table Documento
DocumentoID ExpedienteID    ArchivoID   FechaRegistro   .....
5   1   NULL    2014-11-06 .....

As you can see, the DocumentoID in the Archivo table has 0 as value......and the ArchivoID in the Documento table has NULL...... both entities were saved correctly, except for the relationship...... I have no idea why this happens.... 如您所见,Archivo表中的DocumentoID的值为0 ...而Documento表中的ArchivoID的值为NULL ...除了关系之外,两个实体均已正确保存。 ..我不知道为什么会这样。

You have documentoid in archivo and archivoid in documento. 您在archivo中有documentoid,在documento中有archivoid。 Also documento virtual property in archivo and archivo virtual property in documento. 在archivo中还有documento虚拟属性,在documento中还有archivo虚拟属性。 This is a circular dependency so ef couldn't create the relationship. 这是一个循环依赖关系,因此ef无法创建该关系。 You need to remove one of them depending upon which is primary table and which is child table. 您需要删除其中之一,具体取决于哪个是主表,哪个是子表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM