在这种情况下如何设定流畅的api的一对一的关系? (EF6) [英] How to set the one-to-one relationship with fluent api in this case? (EF6)

查看:160
本文介绍了在这种情况下如何设定流畅的api的一对一的关系? (EF6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个实体:

  public partial class Ficheros 
{
public Guid Idfichero {得到;组; }
public long Iddocumento {get;组; }
public byte [] Fichero {get;组; }

public virtual Documentos IddocumentoNavigation {get;组; }
}

public partial class Documentos
{
public Documentos()
{
ElementosDocumentos = new HashSet< ElementosDocumentos>();
}

public long Iddocumento {get;组; }
public string Nombre {get;组; }
public long? IdtipoDocumento {get;组; }
public string Codigo {get;组; }
public decimal? Espacio {get;组; }
public string Unidades {get;组; }
public long?字节{get;组; }

public virtual ICollection< ElementosDocumentos> ElementosDocumentos {get;组; }
public virtual Ficheros Ficheros {get;组; }
public virtual DocumentosTipos IdtipoDocumentoNavigation {get;组;
}

在数据库中,IDFichero是一个唯一的标识符,在Documentos中,IDDocumento是大型自动增量主表是Documentos,它有唯一的一个fichero,并且它是必需的。



我看到的例子,它会让我IDFichero是IDDocumento,但是要在数据库中存储一个文件,我需要该ID是一个唯一标识符。



谢谢。

解决方案

您在EF中描述的关系是一个一对一的FK关联,需要两端, Documentos 是主体和 Ficheros 依赖



EF不支持显式FK对于这种类型的关联,所以开始删除 Ficheros.Iddocumento 属性:

  public partial class Ficheros 
{
public Guid Idfichero {get;组; }
public byte [] Fichero {get;组; }

public virtual Documentos IddocumentoNavigation {get;组; }
}

然后使用以下流畅配置:

  modelBuilder.Entity< Documentos>()
.HasRequired(e => e.Ficheros)
.WithRequiredPrincipal(e => e.IddocumentoNavigation)
.Map(m => m.MapKey(Iddocumento));


I have this two entities:

    public partial class Ficheros
        {
            public Guid Idfichero { get; set; }
            public long Iddocumento { get; set; }
            public byte[] Fichero { get; set; }

            public virtual Documentos IddocumentoNavigation { get; set; }
        }

public partial class Documentos
    {
        public Documentos()
        {
            ElementosDocumentos = new HashSet<ElementosDocumentos>();
        }

        public long Iddocumento { get; set; }
        public string Nombre { get; set; }
        public long? IdtipoDocumento { get; set; }
        public string Codigo { get; set; }
        public decimal? Espacio { get; set; }
        public string Unidades { get; set; }
        public long? Bytes { get; set; }

        public virtual ICollection<ElementosDocumentos> ElementosDocumentos { get; set; }
        public virtual Ficheros Ficheros { get; set; }
        public virtual DocumentosTipos IdtipoDocumentoNavigation { get; set; }
    }

In the database, IDFichero is an uniqueidentifier and in Documentos the IDDocumento is a big int autoincrement. The main table is Documentos, that has one and only one fichero, and it is requiered.

The examples that I have seen, it would make me that IDFichero was IDDocumento, but to store a file in the database I need that the ID is a uniqueidentifier.

Thanks.

解决方案

The relationship you are describing in EF terms is one-to-one FK association with both ends required, Documentos being the principal and Ficheros the dependent.

EF does not support explicit FK for this type of association, so start by removing the Ficheros.Iddocumento property:

public partial class Ficheros
{
    public Guid Idfichero { get; set; }
    public byte[] Fichero { get; set; }

    public virtual Documentos IddocumentoNavigation { get; set; }
}

then use the following fluent configuration:

modelBuilder.Entity<Documentos>()
    .HasRequired(e => e.Ficheros)
    .WithRequiredPrincipal(e => e.IddocumentoNavigation)
    .Map(m => m.MapKey("Iddocumento"));

这篇关于在这种情况下如何设定流畅的api的一对一的关系? (EF6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆