我有班级
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? DeactivatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public bool IsActive { get; set; }
}
public class Supplier : Company
{
public int ContactId { get; set; }
public int DocumentId { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
}
这些是映射
public class CompanyMap : EntityTypeConfiguration<Company>
{
public CompanyMap()
{
// Primary Key
HasKey(c => c.Id);
//Table
ToTable("Company");
}
}
public class SupplierMap : EntityTypeConfiguration<Supplier>
{
public SupplierMap()
{
// Primary Key
HasKey(s => s.Id);
// Properties
//Relationship
HasRequired(s => s.Company)
.WithMany().HasForeignKey(c => c.CompanyId);
//Table
ToTable("Supplier");
}
}
这是上下文
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new CompanyMap());
modelBuilder.Configurations.Add(new SupplierMap());
}
public DbSet<Company> Companies { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
}
接种:
var companies = new List<Company>
{
new Company {Id = 1, Name = "X", CreatedOn = DateTime.Now, IsActive = true, UpdatedOn = DateTime.Now},
new Company {Id = 2, Name = "XX", CreatedOn = DateTime.Now, IsActive = true, UpdatedOn = DateTime.Now},
new Company {Id = 3, Name = "XXX", CreatedOn = DateTime.Now, IsActive = true, UpdatedOn = DateTime.Now},
new Company {Id = 4, Name = "XXXX", CreatedOn = DateTime.Now, IsActive = true, UpdatedOn = DateTime.Now},
};
foreach (var item in companies)
{
context.Companies.AddOrUpdate(item);
}
var suppliers = new List<Supplier>
{
new Supplier {Id = 1, CreatedOn = DateTime.Now, Company = companies[0], IsActive = true, UpdatedOn = DateTime.Now},
new Supplier {Id = 2, CreatedOn = DateTime.Now, Company = companies[1], IsActive = true, UpdatedOn = DateTime.Now},
new Supplier {Id = 3, CreatedOn = DateTime.Now, Company = companies[2], IsActive = true, UpdatedOn = DateTime.Now},
new Supplier {Id = 4, CreatedOn = DateTime.Now, Company = companies[3], IsActive = true, UpdatedOn = DateTime.Now}
};
foreach (var item in suppliers)
{
context.Suppliers.AddOrUpdate(item);
}
this.SaveChanges(context);
我期待的是2个表,所有表都继承了相同的属性,(基类和继承类但我无法工作,我的结果是一个带有[Discriminator] collunn的表。
任何帮助都非常受欢迎。
此致
答案 0 :(得分:0)
强制TPT足以指定ToTable,因此我复制/粘贴了您的代码,所有内容都符合预期。
这里是使用EF 6.1.3-40302创建表语句
CREATE TABLE [Company] (
[Id] int not null identity(1,1)
, [Name] text null
, [CreatedOn] datetime not null
, [DeactivatedOn] datetime null
, [UpdatedOn] datetime null
, [IsActive] bit not null
);
ALTER TABLE [Company] ADD CONSTRAINT [PK_Company_65cc9e90] PRIMARY KEY ([Id])
CREATE TABLE [Supplier] (
[Id] int not null
, [ContactId] int not null
, [DocumentId] int not null
, [CompanyId] int not null
);
ALTER TABLE [Supplier] ADD CONSTRAINT [PK_Supplier_65cc9e90] PRIMARY KEY ([Id])
CREATE INDEX [IX_Id] ON [Supplier] ([Id])
CREATE INDEX [IX_CompanyId] ON [Supplier] ([CompanyId])
ALTER TABLE [Supplier] ADD CONSTRAINT [FK_Supplier_Company_Id] FOREIGN KEY ([Id]) REFERENCES [Company] ([Id])
ALTER TABLE [Supplier] ADD CONSTRAINT [FK_Supplier_Company_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [Company] ([Id])
您确定这正是您运行的代码吗?代码出错,因此原始代码可能不同......