问题描述
我对多个表使用一种模型,但是当我创建表时,只创建了其中一个。为什么会这样,可以更改吗?
public DbSet<ShopItemProperty_Model> Champagne_TBL { get; set; }
public DbSet<ShopItemProperty_Model> Gin_TBL { get; set; }
public DbSet<ShopItemProperty_Model> Rum_TBL { get; set; }
public DbSet<ShopItemProperty_Model> SparklyWine_TBL { get; set; }
public DbSet<ShopItemProperty_Model> Whisky_TBL { get; set; }
public DbSet<ShopItemProperty_Model> WhiteWine_TBL { get; set; }
public DbSet<ShopItemProperty_Model> RedWine_TBL { get; set; }
public DbSet<ShopItemProperty_Model> Wodka_TBL { get; set; }
解决方法
EfCore 根据您的类型创建表,因此 如果您必须这样做,您可以将一个模型作为一个抽象类,并定义从它继承而来的另一个模型。
此外,我不得不说这个数据库设计可能会更好,我不了解您的业务,但最简单的方法是创建一个带有 enum 等特殊判别器的表
public abstract class ShopItemProperty_Model
{
public int Id { get; set; }
/* Other fields */
}
[Table("Gin_TBL")]
public class Gin_TBL : ShopItemProperty_Model
{
}
[Table("Rum_TBL")]
public class Rum_TBL : ShopItemProperty_Model
{
}
[Table("SparklyWine_TBL")]
public class SparklyWine_TBL : ShopItemProperty_Model
{
}
[Table("Whisky_TBL")]
public class Whisky_TBL : ShopItemProperty_Model
{
}
[Table("WhiteWine_TBL")]
public class WhiteWine_TBL : ShopItemProperty_Model
{
}
[Table("RedWine_TBL")]
public class RedWine_TBL : ShopItemProperty_Model
{
}
[Table("Wodka_TBL")]
public class Wodka_TBL : ShopItemProperty_Model
{
}