Reputation: 1105
I'm using EF Code First with MVC3.
If I don't specify a connection string in my web config the application works fine. On start up it creates the database and tables and everything works fine.
If I specify a connection string for an new empty database I created it doesn't create the tables and I get an error when running the application. The error is essential a null pointer since the table doesn't exist and I'm trying to query it.
Here is my connection string:
<add name="MyContextName" connectionString="Data Source=.\sqlexpress;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
MyContextClass
public class MyContextName : DbContext
{
public DbSet<Book> books{ get; set;}
}
Upvotes: 0
Views: 1118
Reputation: 1105
I found the problem to be with my process. I created a new empty database then copied the connection string to my web config. It seems you're not suppose to create the database at all. Just set the connection string and like EF generate the database for you.
Upvotes: 1
Reputation: 1415
Replace :
<add name="MyContextName" connectionString="Data Source=.\sqlexpress;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
By:
<add name="MyContextName" connectionString="Data Source=|DataDirectory|databasename" providerName="System.Data.SqlClient" />
Upvotes: 2
Reputation: 825
If this your connection string then you have a copy error in it,
you have written "Data Source=" twice,
see below
<add name="MyContextName" connectionString="Data Source=
Data Source=.\sqlexpress;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
Upvotes: 2