C# 具有1:1关系和1:多关系的实体-设置外键

C# 具有1:1关系和1:多关系的实体-设置外键,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我有这个简单的模型,我想配置它们之间的关系 一家公司有一个标志 一个公司可以有很多文件 public class Company { public int Id { get; set; } public File Logo { get; set; } public List<File> Attachments { get; set; } = new List<File>(); } public class File { public

我有这个简单的模型,我想配置它们之间的关系

一家公司有一个标志

一个公司可以有很多文件

public class Company
{
    public int Id { get; set; }

    public File Logo { get; set; }

    public List<File> Attachments { get; set; } = new List<File>();
}

public class File
{
    public int Id { get; set; }

    public Company Company { get; set; }

    public int CompanyId { get; set; }  

    (...)
}

public class CompanyConfiguration : IEntityTypeConfiguration<Company>
{
    public void Configure(EntityTypeBuilder<Company> builder)
    {
        builder
            .HasMany(x => x.Attachments)
            .WithOne(x => x.Company)
            .HasForeignKey(x => x.CompanyId);

        builder
           .HasOne(x => x.Logo)
           .WithOne(x => x.Company)
           .HasForeignKey(x => x.); // x. doesnt show me anything about "File" class. It looks like assembly

    }
}
但即使我把FK的名字作为字符串插入

public class CompanyConfiguration : IEntityTypeConfiguration<Company>
{
    public void Configure(EntityTypeBuilder<Company> builder)
    {
        builder
            .HasMany(x => x.Attachments)
            .WithOne(x => x.Company)
            .HasForeignKey(x => x.CompanyId);

        builder
           .HasOne(x => x.Logo)
           .WithOne(x => x.CompanyOtherProperty)
           .HasForeignKey("CompanyOtherPropertyId");
    }
}
公共类公司配置:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
建设者
.HasMany(x=>x.Attachments)
.有一家(x=>x.公司)
.HasForeignKey(x=>x.CompanyId);
建设者
.HasOne(x=>x.Logo)
.WithOne(x=>x.CompanyOtherProperty)
.HasForeignKey(“公司其他财产ID”);
}
}
System.InvalidOperationException:'您正在配置'Company'和'File'之间的关系,但在'CompanyOtherPropertyId'上指定了外键。必须在属于关系的类型上定义外键。”


考虑到您的需要,我会使用继承来实现这一点,因为Logo和attachement有共同的属性,但关系不同

public abstract class File
{
    public int Id { get; set; }
    .....
}

public class Logo : File
{
    ....
}

public class Attachement : File
{
    public Company Company { get; set; }

    public int CompanyId { get; set; }
}

public class Company
{
    public int Id { get; set; }

    public Logo Logo { get; set; }

    public int LogoId { get; set; }

    public ICollection<Attachement> Attachements { get; set; } = new List<Attachement>();

}
公共抽象类文件
{
公共int Id{get;set;}
.....
}
公共类标识:文件
{
....
}
公共类附件:文件
{
上市公司{get;set;}
public int CompanyId{get;set;}
}
公营公司
{
公共int Id{get;set;}
公共徽标{get;set;}
public int LogoId{get;set;}
公共ICollection附件{get;set;}=new List();
}
公共数据库集公司{get;set;}
公共数据库集附件{get;set;}
公共数据库集徽标{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasDiscriminator();
}

您现在的设计很容易出错,我假设一个文件不能同时包含徽标和附件,这意味着两个公司导航属性中的一个始终为空。您必须在系统中的任何地方执行空检查

对于
文件
徽标

public abstract class File
{
    public int Id { get; set; }
    .....
}

public class Logo : File
{
    ....
}

public class Attachement : File
{
    public Company Company { get; set; }

    public int CompanyId { get; set; }
}

public class Company
{
    public int Id { get; set; }

    public Logo Logo { get; set; }

    public int LogoId { get; set; }

    public ICollection<Attachement> Attachements { get; set; } = new List<Attachement>();

}
public DbSet<Company> Companies { get; set; }

public DbSet<Attachement> Attachements { get; set; }

public DbSet<Logo> Logos { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<File>().HasDiscriminator();
}