2012-09-10 80 views
4

實體框架在使用SQL Express時不會創建我的數據庫,但如果我使用SQL Server CE,則它工作正常。實體框架在使用sqlserver時不創建數據庫

我一直試圖弄清楚這幾天,但它可能是我對ASP.NET和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> 

堆棧跟蹤:

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: 

回答

3

正如它說,你不是在連接字符串中提供的Initial Catalog=__name__

SqlCe是爲應用程序的目的而生成的本地文件 - 不涉及多個數據庫。但是,SQLEXPRESS可以託管和提供多個數據庫,從而提供必要的服務。

以下內容添加到您的connectionString

;Initial Catalog=__name__ 

哪裏__name__是您要使用的數據庫的名稱。

+0

另請參閱http://stackoverflow.com/questions/4116994/sql-express-connection-string-for-entity-framework-code-first –

+0

哇!這工作!謝謝!我很驚訝,因爲我沒有看到任何包括成員資格連接字符串的例子。十分感謝你的幫助! – user1658921

+0

有關其他信息,雖然您提供了一個mdf文件,但SQLEXPRESS仍然需要知道應該將哪個數據庫視爲該文件(爲了這個和將來的參考)。 –

相關問題