entity framework - ASP.NET Core get WebRootPath in class to seed database -


using asp.net core mvc , entity framework 6, want seed code-first database data csv file have placed in wwwroot\data

i trying access webrootpath value in class performs seed cannot work. understand solution based on dependency injection though being new asp.net core , dependency injection haven't got work.

startup.cs - standard code, dbcontext setup found via question.

public class startup {     public startup(ihostingenvironment env)     {         var builder = new configurationbuilder()             .setbasepath(env.contentrootpath)             .addjsonfile("appsettings.json", optional: true, reloadonchange: true)             .addjsonfile($"appsettings.{env.environmentname}.json", optional: true)             .addenvironmentvariables();         configuration = builder.build();     }      public iconfigurationroot configuration { get; }      // method gets called runtime. use method add services container.     public void configureservices(iservicecollection services)     {         // add dbcontext         services.addscoped(p =>         {             var connectionstring = configuration["data:projectdbcontext:connectionstring"];             return new projectdbcontext(connectionstring);         });          // add framework services.         services.addmvc();     }      // method gets called runtime. use method configure http request pipeline.     public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)     {         loggerfactory.addconsole(configuration.getsection("logging"));         loggerfactory.adddebug();          if (env.isdevelopment())         {             app.usedeveloperexceptionpage();             app.usebrowserlink();         }         else         {             app.useexceptionhandler("/home/error");         }          app.usestaticfiles();          app.usemvc(routes =>         {             routes.maproute(                 name: "default",                 template: "{controller=home}/{action=index}/{id?}");         });     } } 

projectdbcontext.cs

[dbconfigurationtype(typeof(dbconfig))] public class projectdbcontext : dbcontext {     static projectdbcontext ()     {         database.setinitializer(new projectinitializer());     }      public projectdbcontext (string connectionname) : base(connectionname)     {      }      public dbset<someentity> someentity{ get; set; }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.conventions.remove<pluralizingtablenameconvention>();         base.onmodelcreating(modelbuilder);     } } 

projectinitializer.cs

public class projectinitializer : dropcreatedatabasealways<projectdbcontext> {     private readonly ihostingenvironment _appenvironment;      public projectinitializer(ihostingenvironment appenvironment)     {         _appenvironment = appenvironment;     }      public override void initializedatabase(projectdbcontext context)     {         base.initializedatabase(context);     }      protected override void seed(projectdbcontext db)     {         string datapath = path.combine(_appenvironment.webrootpath, "data");         string contracts = path.combine(datapath, "data.csv");          // parse file, create objects          db.savechanges();     } } 

i think problem environment not being passed projectinitializer. you'll need inject environment dbcontext , pass projectinitializer. not see 'ihostingenvironment appenvironment' parameter in dbcontext.

by way, aware can use contentrootpath instead of webrootpath?


Comments

Popular posts from this blog

Formatting string according to pattern without regex in php -

c - zlib and gdi32 with OpenSSL? -

java - inputmismatch exception -