4

使用 SQL Express 时,实体框架不会创建我的数据库,但如果我使用 SQL Server CE,它可以正常工作。

几天来我一直在尝试解决这个问题,但这可能是我对 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: 
4

1 回答 1

3

正如它所说,你没有Initial Catalog=__name__在你的连接字符串中提供一个。

SqlCe 是为应用程序目的而创建的本地文件——不涉及多个数据库。但是,SQLEXPRESS 可以托管和提供多个数据库,这使得提供Initial Catalog势在必行。

将以下内容添加到您的connectionString:

;Initial Catalog=__name__

__name__您要使用的数据库的名称在哪里。

于 2012-09-10T01:10:42.437 回答