c# - DbSet Object reference not set to an instance of an object -


i'm trying make universal 3-tier application generic repositories data provider/orm. rewrote code , got stuck on error of object reference not set instance of object while trying make action in dbset. i'm confused.. @ least nudge me in right direction.

repository.cs

 public class repository<t> : irepository<t> t : class {      public iunitofwork unitofwork { get; set; }     private idbset<t> _objectset;      private idbset<t> dbset     {         { return _objectset ?? (_objectset = unitofwork.context.set<t>()); }     } public list<t> getall()     {         try         {             return dbset.tolist();         }         catch (exception)         {             throw new exception("error in repository while attempts elements");         }     } } 

efunitofwork.cs

 public class efunitofwork : iunitofwork {     public efunitofwork()     {         context = new appcontext();     }      public dbcontext context { get; set; }      public void commit()     {         context.savechanges();     }      public bool lazyloadingenabled     {         { return context.configuration.lazyloadingenabled; }         set { context.configuration.lazyloadingenabled = value; }     }      public void dispose()     {         context.dispose();     } } 

appcontext.cs

 public class appcontext : dbcontext, idbcontext {     public appcontext()     {         database.setinitializer(new migratedatabasetolatestversion<appcontext,            configuration>());         configuration.proxycreationenabled = false;     }      public dbset<logic> logics { get; set; }      public dbset<category> categories { get; set; }      public new idbset<tentity> set<tentity>() tentity : class     {         return base.set<tentity>();     } } 

idbcontext.cs

public interface idbcontext {     idbset<tentity> set<tentity>() tentity : class;     int savechanges();     void dispose(); } 

configuration.cs

class configuration : dbmigrationsconfiguration<appcontext> {     public configuration()     {         automaticmigrationsenabled = true;         automaticmigrationdatalossallowed = true;     }      protected override void seed(appcontext context)     {      } } 

i suggest change repository expect iunitofwork in constructor not valid without it.

public class repository<t> : irepository<t> t : class {     private readonly iunitofwork _unitofwork;     public repository(iunitofwork unitofwork;)     {         _unitofwork = unitofwork;     }     public iunitofwork unitofwork { { return _unitofwork; } } } 

idbcontext should defined implementing idisposable (and remove dispose method interface definition)

public interface idbcontext : idisposable {     idbset<tentity> set<tentity>() tentity : class;     int savechanges(); } 

the following line of code appcontext have effect of disabling lazy loading because lazy loading implemented proxies

configuration.proxycreationenabled = false; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -