c# - EF code first create database on start up with sqlclient provider -


i'm trying create database on startup code:

class datacontext : dbcontext {     public dbset<student> students { get; set; }      public datacontext() : base("database")     {         database.initialize(true);      }  }  ///////// try { var db = new datacontext();  db.students.add(new student());  db.savechanges(); }  catch (exception ex) {  } 

//app.config

<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>     <connectionstrings>         <add name="database"             connectionstring="server=(localdb)\v11.0;integrated security=true;             attachdbfilename=c:\users\aaron\documents\visual studio 2015\projects\ef test\ef test\bin\debug\mydata.mdf"             providername="system.data.sqlclient"/>     </connectionstrings>   <startup>     <supportedruntime version="v4.0" sku=".netframework,version=v4.5" />   </startup>   <entityframework>     <defaultconnectionfactory type="system.data.entity.infrastructure.localdbconnectionfactory, entityframework">       <parameters>         <parameter value="mssqllocaldb" />       </parameters>     </defaultconnectionfactory>     <providers>       <provider invariantname="system.data.sqlclient" type="system.data.entity.sqlserver.sqlproviderservices, entityframework.sqlserver" />     </providers>   </entityframework> 

i think connection string bad. i've tried dozen no luck. exception on initialize call. "an attempt attach auto-named database file failed". help.

edit changed this:

<connectionstrings> <add name="database"         connectionstring="server=(localdb)\v11.0;integrated security=true;         database=c:\users\aaron\documents\visual studio 2015\projects\ef test\ef test\bin\debug\mydata.mdf"         providername="system.data.sqlclient"/> </connectionstrings>\ 

and "additional information: cannot open database "c:\users\aaron\documents\visual studio 2015\projects\ef test\ef test\bin\debug\mydata.mdf" requested login. login failed."

see here.

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <connectionstrings>     <add name="schooldbconnectionstring"      connectionstring="data source=(localdb)\v11.0;initial catalog=schooldb-byconnectionstring;integrated security=true"      providername="system.data.sqlclient"/>   </connectionstrings> </configuration> 

so code first create new schooldb-byconnectionstring database.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -