c# - Creating different tables using using same base abstract class -
i have 2 models have same fields, chose make different models them because needed 2 different tables, 1 each.
earlier working fine when had 2 different tables each model, started using abstract base class because code inside both models same.
now have single table comprised of data save.
how can create different tables 2 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 1 shown above abstract base class.
public class article : basegrammar { } public class adjective : basegrammar { }
just if 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 adding context class 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 columns instead of different tables
this set give 2 tables: (1) adjectives (2) articles
the context should 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 naming convention. in .net class names , property names should follow pascal notation. therefore, should be:
basegrammar article adjective ruleapplicable // other properties should follow same convention
Comments
Post a Comment