实体框架连接到SQLEXPRESS而不是SQL Compact [英] Entity Framework connecting to SQLEXPRESS not SQL Compact
问题描述
当我尝试运行该项目时,我收到一个错误,CREATE DATABASE权限被拒绝在数据库'master'。。
我搜索帮助和响应到几个博客尝试标识正在使用的连接字符串。令我惊讶的是,它是
context.Database.Connection.ConnectionStringData Source = .\\\SQLEXPRESS; Initial Catalog = Ancestors.Models.AncestorsContext; Integrated Security = True; MultipleActiveResultSets = True; Application Name = EntityFrameworkMUEstring
即使我的web.config文件包含以下部分...
< connectionStrings>
< add name =DefaultConnectionproviderName =System.Data.SqlServerCe.4.0connectionString =Data Source = | DataDirectory | \aspnet-Ancestors-20121012114712.sdf/>
< / connectionStrings>
< system.data>
< DbProviderFactories>
< remove invariant =System.Data.SqlServerCe.4.0/>
< add name =Microsoft SQL Server Compact Data Provider 4.0invariant =System.Data.SqlServerCe.4.0description =.NET Framework Data Provider for Microsoft SQL Server Compacttype =System.Data SqlServerCe.SqlCeProviderFactory,System.Data.SqlServerCe,Version = 4.0.0.1,Culture = neutral,PublicKeyToken = 89845dcd8080cc91/>
< / DbProviderFactories>
< /system.data>有人有任何想法,为什么EF坚持要创建一个连接到SQLEXPRESS而不是SQL Compact?
我可以在解决方案中的任何地方找到SQLEXPRESS的任何引用。解决方案而不是使用以下内容传递你的连接名称到DBContext
public YourContext():base(DefaultConnection)
{
}
使用名称指令
public $($)
这将告诉EF将参数用作连接。
还添加clear 在web.config的connectionStrings部分顶部可能会解决继承问题。
I have been trying out a code first EF project with SQl Compact 4.0. When I try running the project I get an error that "CREATE DATABASE permission denied in database 'master'.".
I searched for help and in response to a few blogs tried to identify the connection string being used. To my surprise it was
context.Database.Connection.ConnectionString "Data Source=.\\SQLEXPRESS;Initial Catalog=Ancestors.Models.AncestorsContext;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE" string
even though my web.config file includes the following sections...
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\aspnet-Ancestors-20121012114712.sdf" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
Has anyone any idea why EF is insisting on creating a connection to SQLEXPRESS instead of SQL Compact? I can find no reference at all to SQLEXPRESS anywhere in the solution.
Instead of using the following to pass your connection name to the DBContext
public YourContext() : base("DefaultConnection")
{
}
Use the name directive
public YourContext() : base("name=DefaultConnection")
{
}
This will tell EF to use the parameter as a connection.
Also Adding the "clear" at the top of your connectionStrings sections in web.config may resolve inheritance issues.
这篇关于实体框架连接到SQLEXPRESS而不是SQL Compact的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!