entity framework - cannot find default auto generated connection String in web config file in ASP.Net MVC 5 -


here web config file after extending dbcontext class in schoolcontext class,

   <?xml version="1.0" encoding="utf-8"?>     <!--       more information on how configure asp.net application, please visit       http://go.microsoft.com/fwlink/?linkid=301880       -->     <configuration>       <configsections>         <!-- more information on entity framework configuration, visit http://go.microsoft.com/fwlink/?linkid=237468 -->         <section name="entityframework" type="system.data.entity.internal.configfile.entityframeworksection, entityframework, version=6.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" />       </configsections>       <appsettings>         <add key="webpages:version" value="3.0.0.0" />         <add key="webpages:enabled" value="false" />         <add key="clientvalidationenabled" value="true" />         <add key="unobtrusivejavascriptenabled" value="true" />       </appsettings>       <system.web>         <compilation debug="true" targetframework="4.5" />         <httpruntime targetframework="4.5" />       </system.web>       <runtime>         <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">           <dependentassembly>             <assemblyidentity name="microsoft.owin" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="microsoft.owin.security.oauth" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="microsoft.owin.security.cookies" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="microsoft.owin.security" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="newtonsoft.json" culture="neutral" publickeytoken="30ad4fe6b2a6aeed" />             <bindingredirect oldversion="0.0.0.0-6.0.0.0" newversion="6.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="system.web.optimization" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-1.1.0.0" newversion="1.1.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="webgrease" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-1.5.2.14234" newversion="1.5.2.14234" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="system.web.helpers" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="system.web.webpages" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" />           </dependentassembly>           <dependentassembly>             <assemblyidentity name="system.web.mvc" publickeytoken="31bf3856ad364e35" />             <bindingredirect oldversion="1.0.0.0-5.2.2.0" newversion="5.2.2.0" />           </dependentassembly>         </assemblybinding>       </runtime>       <entityframework>         <defaultconnectionfactory type="system.data.entity.infrastructure.sqlconnectionfactory, entityframework" />         <providers>           <provider invariantname="system.data.sqlclient" type="system.data.entity.sqlserver.sqlproviderservices, entityframework.sqlserver" />         </providers>       </entityframework>     </configuration> 

here schoolcontext class,

  using system;     using system.collections.generic;     using system.data.entity;     using system.linq;     using system.web;      namespace myonlineshoppingstore.models     {         public class schoolcontext : dbcontext         {             public dbset<student> students { get; set; }         }     } 

why default connection string not generated scenario? possible reasons ? how can specify database connect using code first approach? please help.

is necessary set attributes in application web this, if not choose basic template .net not generate automatically because unknown ubication of database.

one form writing directly file web.config tell database

<connectionstrings>     <add name="defaultconnection" connectionstring="your connection string" providername="system.data.sqlclient" />   </connectionstrings> 

or second is, configure app of way:

open global.asax , in method application_start() add this

database.setinitializer<schoolcontext >(null); try{     using (var context = new schoolcontext ())     {         if (!context.database.exists())         {             // create database include add connection string in web.config             ((iobjectcontextadapter)context).objectcontext.createdatabase();         }     } }catch (exception ex){     throw new invalidoperationexception("an error", ex); } 

the connection string point internal database of application


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -