Search code examples
c#entity-frameworkdbset

Why is only one table created?


I use one model for several tables, but when I create the tables, only one of them is created. Why is that and can it be changed?

  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; }

Solution

  • EfCore Create table based on your types, so If you have to do this you can take one model as a abstract class and define another models which inherited from that.

    Also i have to say this database design it could be better , i don't know your business but easiest way is create a table with special descriminator like 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
    {
    }