4

The Entity Framework will not create my database when using SQL Express but it works fine if I use SQL Server CE.

I have been trying to figure this out for a couple of days now, but it is probably my inexperience with ASP.NET and MVC.

 ConnectionStrings:
<connectionStrings>
    <!--
    <add name="CRM"
         connectionString="data source=|DataDirectory|CRM.sdf"
         providerName="System.Data.SqlServerCe.4.0" />-->

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

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

StackTrace:

System.InvalidOperationException was caught
  Message=Unable to complete operation. The supplied SqlConnection does not specify an initial catalog.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.SqlClient.SqlProviderServices.GetDatabaseName(SqlConnection sqlConnection)
       at System.Data.SqlClient.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
       at System.Data.Objects.ObjectContext.CreateDatabase()
       at System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext)
       at System.Data.Entity.Database.Create()
       at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
       at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3()
       at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
       at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
       at System.Data.Entity.DbSet`1.Add(TEntity entity)
       at MvcApplication1.Controllers.CustomerController.Create(Customer customer) in C:\Users\hlcole\Documents\RegProjects\MvcApplication1\MvcApplication1\Controllers\CustomerController.cs:line 55
  InnerException: 
tereško
  • 58,060
  • 25
  • 98
  • 150
user1658921
  • 65
  • 1
  • 4

1 Answers1

3

Just as it says, you're not supplying an Initial Catalog=__name__ in your connection string.

SqlCe is a local file spun up for the purposes of the application--there are no multiple databases involved. However, SQLEXPRESS can host and offer multiple databases making supplying the Initial Catalog imperative.

Add the following to your connectionString:

;Initial Catalog=__name__

Where __name__ is the name of the database you want to use.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • See also http://stackoverflow.com/questions/4116994/sql-express-connection-string-for-entity-framework-code-first – Brad Christie Sep 10 '12 at 01:12
  • Whoa! That worked!! Thanks! I am very surprised because I haven't seen that in any of the examples including the membership connection string. Thank you so much for the help! – user1658921 Sep 10 '12 at 01:20
  • And for additional information, though you're supplying an mdf file SQLEXPRESS still needs to know what database it should consider the file to be spun up as (for this and future references). – Brad Christie Sep 10 '12 at 01:28