gpt4 book ai didi

asp.net-mvc - 使用 sqlserver Express 时 Entity Framework 不创建数据库

转载 作者:行者123 更新时间:2023-12-02 05:42:51 26 4
gpt4 key购买 nike

使用 SQL Express 时, Entity Framework 不会创建我的数据库,但如果我使用 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:

最佳答案

正如它所说,您没有在连接字符串中提供 Initial Catalog=__name__

SqlCe 是为了应用程序的目的而创建的本地文件——不涉及多个数据库。但是,SQLEXPRESS 可以托管并提供多个数据库,因此必须提供初始目录

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

;Initial Catalog=__name__

其中 __name__ 是您要使用的数据库的名称。

关于asp.net-mvc - 使用 sqlserver Express 时 Entity Framework 不创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com