Connection string for Entity Framework Code First in SQL Server Express

code-first connection-string entity-framework sql-server-express

Question

I'm utilizing the Entity Framework code-first CTP and Visual Web Developer 2010 Express to complete my task. With the new SQL Server CE, I can do this; but, I am unable to locate a connection string that will operate with SQL Server Express.

This one works well while using the SQL Server CE beta (database is created and recreated on model changes).

<add name="TrempimModel"
     connectionString="data source=|DataDirectory|TrempimModel.sdf"
     providerName="System.Data.SqlServerCe.4.0" />

This one is a copy I made from theaspnetdb relationships string,

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
     AttachDBFilename=|DataDirectory|TrempimModel.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

upon launch, displays the following message:

Unable to complete operation. The supplied SqlConnection does not specify an initial catalog

So how can you get SQL Server Express to function with it?

1
38
6/14/2014 3:19:13 PM

Accepted Answer

Your connection string has the following issue:

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.sdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

You are essentially specifying the "server" to which you are connecting, but you are really specifying which database within the file to connect to (not). Additionally, SQL Server Express database files have the following file extension:.mdf (not .sdf You must also consider SQL Server Compact Edition (that's what it's called). (was a typo, as stated in the OP's comment).

You must provide an additionaldatabase=.... (or Initial Catalog=..... (in the connection string:

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
                       database=YourDatabaseName;
                       AttachDBFilename=|DataDirectory|aspnetdb.mdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

Then, everything ought to operate without a hitch.

The ConnectionStrings website has a ton of examples and further background information.

61
11/7/2010 10:25:06 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow