Entity framework 6 在这种情况下,如何设置与fluent api的一对一关系?(EF6)
我有两个实体:Entity framework 6 在这种情况下,如何设置与fluent api的一对一关系?(EF6),entity-framework-6,one-to-one,ef-fluent-api,Entity Framework 6,One To One,Ef Fluent Api,我有两个实体: public partial class Ficheros { public Guid Idfichero { get; set; } public long Iddocumento { get; set; } public byte[] Fichero { get; set; } public virtual Documentos IddocumentoNa
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; }
}
公共部分类Ficheros
{
公共Guid Idfichero{get;set;}
公共长Iddocumento{get;set;}
公共字节[]Fichero{get;set;}
公共虚拟文档OS IddocumentoNavigation{get;set;}
}
公共部分类文档
{
公共文件()
{
ElementosDocumentos=newhashset();
}
公共长Iddocumento{get;set;}
公共字符串Nombre{get;set;}
公共long?IdtipoDocumento{get;set;}
公共字符串Codigo{get;set;}
公共十进制数?Espacio{get;set;}
公共字符串Unidades{get;set;}
公共长字节{get;set;}
公共虚拟ICollection元素OSDocumentOS{get;set;}
公共虚拟Ficheros Ficheros{get;set;}
公共虚拟文档ostipos IdtipoDocumentoNavigation{get;set;}
}
在数据库中,IDFichero是一个唯一标识符,而在Documentos中,IDDocumento是一个大的int自动增量。主表是Documentos,它只有一个fichero,并且是必需的
我所看到的例子会让我觉得IDFichero是IDDocumento,但要在数据库中存储文件,我需要ID是唯一标识符
谢谢。您在EF术语中描述的关系是一对一的FK关联,两端都需要,
Documentos
是主体,Ficheros
是从属关系
EF不支持这种类型的关联的显式FK,因此请先删除Ficheros.Iddocumento
属性:
public partial class Ficheros
{
public Guid Idfichero { get; set; }
public byte[] Fichero { get; set; }
public virtual Documentos IddocumentoNavigation { get; set; }
}
然后使用以下fluent配置:
modelBuilder.Entity<Documentos>()
.HasRequired(e => e.Ficheros)
.WithRequiredPrincipal(e => e.IddocumentoNavigation)
.Map(m => m.MapKey("Iddocumento"));
modelBuilder.Entity()
.HasRequired(e=>e.Ficheros)
.WithRequiredPrincipal(e=>e.ID文档激活)
.Map(m=>m.MapKey(“Iddocumento”);
您在EF术语中描述的关系是一对一的FK关联,需要两端,Documentos
是主体,Ficheros
是从属关系
EF不支持这种类型的关联的显式FK,因此请先删除Ficheros.Iddocumento
属性:
public partial class Ficheros
{
public Guid Idfichero { get; set; }
public byte[] Fichero { get; set; }
public virtual Documentos IddocumentoNavigation { get; set; }
}
然后使用以下fluent配置:
modelBuilder.Entity<Documentos>()
.HasRequired(e => e.Ficheros)
.WithRequiredPrincipal(e => e.IddocumentoNavigation)
.Map(m => m.MapKey("Iddocumento"));
modelBuilder.Entity()
.HasRequired(e=>e.Ficheros)
.WithRequiredPrincipal(e=>e.ID文档激活)
.Map(m=>m.MapKey(“Iddocumento”);