如何在一对多关系中定义自定义表名?

时间:2016-11-30 16:26:29

标签: c# entity-framework

我使用EF Core并拥有3个实体:FormableLineEntity , LabLineEntity, FileRelatedEntity。 说FormableLineEntity的集合为FileRelatedEntitiesLabLineEntity的集合为FileRelatedEntities

以下是上下文的部分:

public class FormManagerContext : DbContext
{
    private IConfigurationRoot m_config;

    public FormManagerContext(IConfigurationRoot p_config, DbContextOptions<FormManagerContext> p_options) : base(p_options)
    {
        m_config = p_config;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder p_optionsBuilder)
    {
        base.OnConfiguring(p_optionsBuilder);
        string connectionString = m_config["DatabaseSettings:ConnectionString"];
        p_optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("ServiTechDatabaseMain"));
    }
    public DbSet<FormableEntity> Forms { get; set; }
    public DbSet<FormableLineEntity> FormableLines { get; set; }

    protected override void OnModelCreating(ModelBuilder p_builder)
    {
        p_builder.Entity<QuotationEntity>();
        base.OnModelCreating(p_builder);

        p_builder.Entity<FormableEntity>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
        p_builder.Entity<FormableLineEntity>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
    }
}

FormableEntitypublic ICollection<RelatedFileEntity> RelatedFiles { get; } = new List<RelatedFileEntity>();

的地方
public class LabContext : DbContext
{
    private IConfigurationRoot m_config;

    public LabContext(IConfigurationRoot p_config, DbContextOptions<LabContext> p_options) : base(p_options)
    {
        m_config = p_config;
    }

    public DbSet<LabItemEntity> LabItems { get; set; }
    public DbSet<LabMainEntity> LabMains { get; set; }
    public DbSet<LabLineEntity> LabLines { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder p_optionsBuilder)
    {
        base.OnConfiguring(p_optionsBuilder);
        string connectionString = m_config["DatabaseSettings:ConnectionString"];
        p_optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("ServiTechDatabaseMain"));
    }
    protected override void OnModelCreating(ModelBuilder p_builder)
    {
        base.OnModelCreating(p_builder);

        p_builder.Entity<LabItemEntity>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
        p_builder.Entity<LabLineEntity>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
        p_builder.Entity<LabMainEntity>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
    }
}

LabLineEntity还有public ICollection<RelatedFileEntity> RelatedFiles { get; } = new List<RelatedFileEntity>();

当我使用dotnet ef database update -c FormManagerContext Context更新我的数据库时,确定。

当我使用dotnet ef database update -c LabContext时,我会抱怨:

  

已经有一个名为&#39; RelatedFileEntity&#39;在数据库中。

  • 我无法找到如何强制EF使用我的自定义名称来命名这些表?
  • 也许用一张桌子?因为唯一改变的是FK名称。有可能吗?

解决方案?此致!

1 个答案:

答案 0 :(得分:0)

这些是不同的实体,因此映射到不同的表。 为“RelatedFiles”创建表时,不能将一个属性(外键)映射到多个表。