我使用EF Core并拥有3个实体:FormableLineEntity , LabLineEntity, FileRelatedEntity
。
说FormableLineEntity
的集合为FileRelatedEntities
,LabLineEntity
的集合为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()");
});
}
}
FormableEntity
有public 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;在数据库中。
解决方案?此致!
答案 0 :(得分:0)
这些是不同的实体,因此映射到不同的表。 为“RelatedFiles”创建表时,不能将一个属性(外键)映射到多个表。