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

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

问题描述

我有这两个实体:

    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; }
    }

在数据库中,IDFichero 是一个唯一标识符,而在 Documentos 中,IDDocumento 是一个大的 int 自动增量.主表是 Documentos,它只有一个 fichero,它是必需的.

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.

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

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.

谢谢.

推荐答案

你用 EF 术语描述的关系是一对一的 FK 关联,需要两端,DocumentosprincipalFicheros 从属.

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 不支持此类关联的显式 FK,因此首先要删除 Ficheros.Iddocumento 属性:

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"));

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

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