c# - MVC 4 Entity Framework Cannot Connect to Dynamically Created Database from IIS Server -


after hosted apps in localhost iis, system gives me error:

cannot open database "alvincms" requested login. login failed. login failed user 'nt authority\system'.

the database alvincms created @ runtime. system has no problem when in debug mode.

here global.asax:

public class mvcapplication : system.web.httpapplication {     protected void application_start()     {         //check , init database         alvin_cms.app_start.databaseconfig.initialize();         ...... 

this initialization:

public static void initialize() {     alvin_cms.models.alvincmsmigrationdbcontext migrationdb = new models.alvincmsmigrationdbcontext();     try     {         if (!migrationdb.database.exists())         {             migrationdb.database.initialize(false); //this creates database             alvincmsextension.models.accountdbcontext accountdb = new alvincmsextension.models.accountdbcontext();             accountdb.database.initialize(false);             setdefaultvalue(migrationdb);         }         migrationdb.database.initialize(false);     }     catch (exception e)     {         migrationdb.database.delete();         alvincmsextension.helper.log(e);     } } 

here context:

public alvincmsmigrationdbcontext()         : base("defaultconnection") {     database.setinitializer<alvincmsmigrationdbcontext>(new createdatabaseifnotexists<alvincmsmigrationdbcontext>()); } 

i set apps pool use localsystem , have integratedsecurity=true in connection string. how can fix error?

note: if run on visual studio development server, runs fine no error. if use localdb , run on iis express, runs fine too. problem exist if use iis, when debug program visual studio local iis web server, error produced


update

after 4 hours of debugging, know wrong. error exists because database not exist. have database error causes code enter exception, database not being created.

the problematic database has connection string:

data source=.\sqlexpress;attachdbfilename="c:\users\public\documents\projects\alvin cms project\alvin cms\alvin cms\app_data\db\backup\alvincms_default.mdf";initial catalog=alvincms_default;integrated security=true;multipleactiveresultsets=true

when hosted on iis, program cannot attach database. causes error:

{"unable open physical file \"c:\users\public\documents\projects\alvin cms project\alvin cms\alvin cms\app_data\db\backup\alvincms_default.mdf\". operating system error 5: \"5(access denied.)\".\r\ncannot attach file 'c:\users\public\documents\projects\alvin cms project\alvin cms\alvin cms\app_data\db\backup\alvincms_default.mdf' database 'alvincms_default'."}

but when use visual development server, works fine. new question arises, how can safely attach database without causing errors when hosted on iis server?

you can check following things:

  1. add integrated security=sspi in web.config

  2. allow nt authority/system server role sysadmin.

    goto security > logins > select nt authority\system

enter image description here

give necessary permissions needs app.

enter image description here


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -