基于相同键的实体框架关系

时间:2021-01-12 23:37:08

标签: c# mysql entity-framework

我试图弄清楚是否可以在实体框架中表示一个不太常见的模型。我使用的数据库有 Persons、PersonFile、Files、Images 和 Executables。一个Person可以有多个PersonFiles,每个PersonFile有一个File,一个File要么是Image,要么是Executable(所有这些都有自己的表,但是Images/Executable与File共享主键,而不是有明确的外键关系)

我的人看起来像这样:

public class Person {

  // Properties
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  [JsonProperty("id")]
  public Guid Id { get; set; }

  public virtual ICollection<PersonFile> PersonFile { get; set; }

}

public class Person_Config : IEntityTypeConfiguration<Person> {
  public void Configure(EntityTypeBuilder<Person> entity) {
    entity.ToTable("person");

    // Properties
    entity.Property(e => e.Id)
      .HasColumnName("id");

  }
}

我的 PersonUploads 看起来像这样:

public class PersonFile {

  // Ids
  public Guid FileId { get; set; }
  public Guid PersonId { get; set; }


  // Relations
  public virtual File File { get; set; }
  public virtual Person Person { get; set; }
}

public class PersonFile_Config : IEntityTypeConfiguration<PersonFile> {
  public void Configure(EntityTypeBuilder<PersonFile> entity) {
    entity.ToTable("_Person_File");

    entity.HasKey(e => new { e.FileId, e.PersonId });

    entity.Property(e => e.FileId)
      .HasColumnName("File_id");

    entity.Property(e => e.PersonId)
      .HasColumnName("Person_id");

    entity.HasOne(d => d.File)
      .WithMany(p => p.PersonFile)
      .HasForeignKey(d => d.FileId)
      .OnDelete(DeleteBehavior.ClientSetNull);

    entity.HasOne(d => d.Person)
      .WithMany(p => p.PersonFile)
      .HasForeignKey(d => d.PersonId)
      .OnDelete(DeleteBehavior.ClientSetNull);
  }
}

我的文件模型如下所示:

public class File {
  // Properties
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid Id { get; set; }
  public string Type { get; set; }

  // Relations
  public virtual Image Image { get; set; }
  public virtual Executable Executable { get; set; }

  public virtual ICollection<PersonFile> PersonFile { get; set; }
}

public class File_Config : IEntityTypeConfiguration<File> {
  public void Configure(EntityTypeBuilder<File> entity) {
    entity.ToTable("File");

    // Properties
    entity.Property(e => e.Id)
      .HasColumnName("id")
      .IsRequired();

    entity.Property(e => e.Type)
      .HasColumnName("type")
      .HasMaxLength(255)
      .IsRequired();

  }
}

为了简洁起见,我的 Image 和 Executables 看起来几乎相同,重要的是它们每个与文件的关系。这是图像的样子:

public class Image {
  // Properties
  public Guid Id { get; set; }

  // Relations
  public virtual File File { get; set; }
}

public class Image_Config : IEntityTypeConfiguration<Image> {
  public void Configure(EntityTypeBuilder<Image> entity) {
    entity.ToTable("Image");

    entity.Property(e => e.Id)
      .HasColumnName("id")
      .IsRequired();

    entity.HasOne(u => u.File)
      .WithOne(r => r.Image)
      .HasForeignKey<File>(u => u.Id)
      .OnDelete(DeleteBehavior.ClientSetNull);
  }
}

在使用 linq 查询时,我可以通过执行以下操作按个人检索文件没问题:

ICollection<File> files_image = await (from file in db.File
                                      join image in db.Image
                                      on file.Id equals image.Id
                                      join personfile in db.PersonUpload
                                      on file.Id equals personfile.UploadId
                                      where personfile.PersonId == id
                                      select file)
                                    .ToListAsync();

但是,一旦我尝试使用连接专门查询图像,并选择该图像作为结果类型,如下所示:

ICollection<Image> files_image = await (from file in db.File
                                                  join image in db.Image
                                                  on file.Id equals image.Id
                                                  join personfile in db.PersonFile
                                                  on file.Id equals personfile.FileId
                                                  where personfile.PersonId == id
                                                  select image).ToListAsync();

我收到以下错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'r.PersonId' in 'field list'

有谁知道我哪里出错了,或者如何正确地建立这种关系的模型? (我认为这就是问题所在,但也许我完全遗漏了其他东西)感谢您的帮助!

0 个答案:

没有答案
相关问题