0

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

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • `How can i create different tables for those 2 models.` By creating tables for each? It's not clear why you cannot do this. –  Jul 25 '17 at 19:43
  • Does your context have 2 `DbSet` properties, one for `article` and one for `adjective` or does it just have `baseGrammar`? – DavidG Jul 25 '17 at 19:45
  • It has 2 dbset properties one for article and another for adjective @DavidG –  Jul 25 '17 at 20:07
  • When using a base class only one table is being made that consist of data of both the 2 models @Will –  Jul 25 '17 at 20:08
  • OOoooh, I think you want the Table per Type inheritance strategy -- http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx –  Jul 25 '17 at 20:13
  • Also can someone help me with this error
    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[StructureSSC.Models.baseGrammar]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[StructureSSC..Models.article]'. @Will or @DavidG
    –  Jul 25 '17 at 20:14
  • That's completely different. Look at the type of what you're trying to put in the dictionary. –  Jul 25 '17 at 20:18
  • Refer [this answer](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) to address the error in your last comment. –  Jul 25 '17 at 22:56

1 Answers1

0

This set up will give you 2 tables: (1) adjectives (2) articles

The context should be like this:

public class SomeContext : DbContext
{
    public SomeContext()
        : base("name=SomeContext")
    {
    }
    public virtual DbSet<article> Articles { get; set; }
    public virtual DbSet<adjective> Adjectives { get; set; }
}

public abstract class baseGrammar
{
    //... common properties/columns
}


public class article : baseGrammar
{

}
public class adjective : baseGrammar
{
}

Please note the naming convention. In .NET class names and property names should follow Pascal Notation. Therefore, they should be:

BaseGrammar
Article
Adjective
RuleApplicable // other properties should follow same convention
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • I added virtual keyword in my context file,other than that all code was same. I have added some more info in the question now. The problem is still not resolved. @CodingYoshi –  Jul 27 '17 at 13:38