【问题标题】:EntityFrameworkCore Mapping RelationshipsEntityFrameworkCore 映射关系
【发布时间】:2017-11-14 16:05:54
【问题描述】:

我有以下两个实体:

public class Company
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
    public Guid Id { get; set; }
    public string Town { get; set; }
    public string Country { get; set; }
}

这对应于两个数据库表:

Company:
  Id uniqueidentifier
  Name varchar

Address:
  Id uniqueidentifier
  Town varchar
  Country varchar
  RelationId uniqueidentifier

RelationId 是将链接存储回 CompanyId 的外键。

不能更改类或表。

我试图弄清楚如何在代码优先的 EFCore 中表示此构造。

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Company>().ToTable("Companies");
        modelBuilder.Entity<Address>().ToTable("Addresses");

        modelBuilder.Entity<Company>().HasKey(c => c.Id);
        modelBuilder.Entity<Address>().HasKey(c => c.Id);

        ???????????????????????
    }

我在上面的代码中缺少什么来防止创建 CompanyId 外键并改用“RelationId”。

【问题讨论】:

    标签: c# entity-framework ef-code-first entity-framework-core


    【解决方案1】:

    按照@TanvirArjel 的目的,在地址中创建Guid RelationId 和Company 公司属性,并使用以下流畅配置:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        .....
    
        modelBuilder.Entity<Company>()
            .HasOne(c => c.Address)
            .WithOne(a => a.Company)
            .HasForeignKey(a => a.RelationId);
    }
    

    如果一个地址可以有多个公司,请将WithOne 更改为WithMany 并将公司属性类型更改为ICollection&lt;Company&gt;

    【讨论】:

      【解决方案2】:

      这是满足您要求的解决方案:

      public class Company
      {
          public Guid Id { get; set; }
          public string Name { get; set; }
      
          //remove virtual keyword as there is no lazy loading in  entityframework core
          public ICollection<Address> Addresses { get; set; }
      }
      
      public class Address
      {
          public Guid Id { get; set; }
      
          [ForeignKey("Company ")]
          public Guid RelationId { get; set; }
          public string Town { get; set; }
          public string Country { get; set; }
      
          //remove virtual keyword as there is no lazy loading in  entityframework core
          public Company Company {get; set;}
      }
      

      使用 Fluent API:

      public class YourDbContext : DbContext
      {
          public DbSet<Company> Companies { get; set; }
          public DbSet<Address> Addresses { get; set; }
      
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity<Address>()
                  .HasOne(p => p.Company)
                  .WithMany(b => b.Addresses);
          }
      }
      

      【讨论】:

      • 有没有办法在流畅的映射而不是数据注释中做到这一点?
      • 是的!但是,当数据注释正确地服务于您的目的时,您为什么需要呢??
      • 为了将映射排除在域类之外,这也是团队文化,我想遵循这一点。
      • 在这种情况下你也不需要流畅的配置。 EF会自动拾取一对导航并形成一对多的关系。
      猜你喜欢
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多