Try to create different tables for different models that use the same basic abstract class

advertisements

I have 2 models which have exactly same fields, but I chose to make different models for them because I needed two different tables, one for each.

Earlier everything was working fine when I had two different tables for each model, but then I started using abstract base class because the code inside both the models were same.

Now I have a single table comprised of all the data that I save.

How can I create different tables for those two models.

 public abstract class baseGrammar
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string question { get; set; }
    [Required]
    public string ans { get; set; }
    public string ruleId { get; set; }
    public string ruleApplicable { get; set; }
    [ForeignKey("ruleId")]
    public virtual ruleTable RuleTable { get; set; }
}

The one shown above is my abstract base class.

public class article : baseGrammar
{

}
 public class adjective : baseGrammar
{
}

Just if someone intrested in ruleTable model.

public class ruleTable
{
    [Key]
    public string ruleId { get; set; }
    public string topic { get; set; }
    public string rule { get; set; }
    public string example { get; set; }
    public virtual ICollection<baseGrammar> BaseGrammar { get; set; }
}

Am also adding context class so as to provide better description

public class english : DbContext
{
    public english() : base("name=localServerEng")
    {
        Database.SetInitializer<DbContext>(null);
        Database.SetInitializer<english>(new UniDBInitializer<english>());
    }

    public virtual DbSet<adjective> adjectiveDb { get; set; }
    public virtual DbSet<adverb> adverbDb { get; set; }
    public virtual DbSet<alternativeVerb> alternativeVerbDb { get; set; }
    public virtual DbSet<antonyms> antonymsDb { get; set; }
    public virtual DbSet<article> articleDb { get; set; }
private class UniDBInitializer<T> : DropCreateDatabaseIfModelChanges<english>
    {
    }

    public System.Data.Entity.DbSet<StructureSSC.Areas.AreaEnglish.Models.baseGrammar> baseGrammars { get; set; }
}

Image of SQL Server showing 1 table comprising of all columns instead of different tables


Add table data annotation:

[Table("TABLE_NAME")]

Your class will look like:

[Table("articles")]
public class article : baseGrammar
{

}