c#实体框架:可选的一对一关系

本文关键字:一对一 关系 实体 框架 | 更新日期: 2023-09-27 18:11:20

我有一个文件表,其中包含一个url到一个文件在互联网上的某个地方,或者它需要是一个物理文件存储到一个varbinary(max)列。我创建了第二个表来存储实际的varbinary(max),但是我从来没有将一个映射到一个,并且当我试图保存时,它会向我抛出一个错误,即FileContentId无效。

这些是我的模型和地图:

public class FileModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string FileContentId { get; set; }
    public string Url { get; set; }
    public string CreatedById { get; set; }
    public DateTime CreatedOn { get; set; }
    public virtual ICollection<FileTable> FileTables { get; set; }
    public virtual User CreatedBy { get; set; }
    public virtual FileContent FileContent { get; set; }
}
public class FileContent
{
    public string Id { get; set; }
    public byte[] Contents { get; set; }
    public virtual FileModel FileModel { get; set; }   
}
public class FileMap : EntityTypeConfiguration<FileModel>
{
    public FileMap()
    {
        ToTable("Files");
        HasKey(t => t.Id);
        Property(t => t.Id);
        Property(t => t.Name);
        Property(t => t.FileContentId);
        Property(t => t.Url);
        Property(t => t.CreatedById);
        Property(t => t.CreatedOn);
        // Relationships
        HasRequired(t => t.CreatedBy)
            .WithMany(t => t.Files)
            .HasForeignKey(t => t.CreatedById);
        HasOptional(t => t.FileContent)
            .WithRequired(t => t.FileModel)
            .Map(t => t.MapKey("FileContentId"));
    }
}
抛出的错误是

列名'FileContentId'无效

我确信这是.Map,因为我在值的末尾添加了一个s,它改变了错误。

c#实体框架:可选的一对一关系

实际上我正要发布同样的事情:)。在一对一关系中,您不需要指定ForeignKey,因为一个表中的键将在两个表之间使用。所以如果你把代码改成这个你会得到相同的结果

 public class FileMap : EntityTypeConfiguration<FileContent>
 {
    public FileMap()
   {
    ToTable("FileContent");
    HasKey(t => t.Id);


    WithRequired(t => t.FileModel)
        .HasOptional(t => t.FileContent)

   }
}  

或者您可以使用DataAnnotations(这里您应该指定饶恕键。

  public class FileModel
{
 [Key, ForeignKey("FileContent")]
public string Id { get; set; }
public string Name { get; set; }
public string FileContentId { get; set; }
public string Url { get; set; }
public string CreatedById { get; set; }
public DateTime CreatedOn { get; set; }
public virtual ICollection<FileTable> FileTables { get; set; }
public virtual User CreatedBy { get; set; }
public virtual FileContent FileContent { get; set; }
 }
  public class FileContent
 {
   public string Id { get; set; }
   public byte[] Contents { get; set; }
   public virtual FileModel FileModel { get; set; }   
  }

通过尝试和错误我解决了这个问题,尽管我无法解释当我拉时EF是如何填充FileModel的filcontent的。如果有人能解释清楚,我会将其标记为可接受的答案。

解决方案仅仅是从关系中删除。map。

        public FileMap()
    {
        ToTable("Files");
        HasKey(t => t.Id);
        Property(t => t.Id);
        Property(t => t.Name);
        Property(t => t.FileContentId);
        Property(t => t.Url);
        Property(t => t.CreatedById);
        Property(t => t.CreatedOn);

        // Relationships
        HasRequired(t => t.CreatedBy)
            .WithMany(t => t.Files)
            .HasForeignKey(t => t.CreatedById);
        HasOptional(t => t.FileContent)
            .WithRequired(t => t.FileModel);

    }