I'm trying to create a database on startup with this 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>
<!-- For 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 my connection string is bad. I've tried about a dozen with no luck. I get an exception on my Initialize call. "An attempt to attach an auto-named database for file failed". thanks for the 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 now i get "Additional information: Cannot open database "C:\\Users\\Aaron\\Documents\\visual studio 2015\\Projects\\EF Test\\EF Test\\bin\\Debug\\MyData.mdf" requested by the login. The login failed."
Try
<connectionStrings>
<add name="Database"
connectionString="Data Source=(LocalDb);Database=MyData.mdf;Trusted_Connection=True" />
</connectionStrings>
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 will create a new SchoolDB-ByConnectionString database.
Figured it out. So I created a local database in visual studios Server Explorer->Connect to database. Once I created the database I copied the connection string that looks like this:
<add name="dbtest" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Aaron\Desktop\mydb.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and bam worked like a charm. Thanks all for the help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.