关键字不支持:'数据源':EF代码首先使用ObjectContext和LocalDB [英] Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB
问题描述
这是我的连接字符串:
< add name =connStrproviderName =System.Data.SqlClientconnectionString =Data Source =(LocalDb)\v11。 0; Initial Catalog = proj1db; Integrated Security = True/>
这是尝试创建ObjectContext实例的代码:
var connectionString = ConfigurationManager
.ConnectionStrings [connStr]
.ConnectionString;
ObjectContext _context = new ObjectContext(connectionString);
最后一行抛出System.ArgumentException:不支持关键字:'data source'。
我正在使用Visual Studio 2012进行Web和目标.NET Framework 4.5。我的机器上安装了LocalDB。
如果我使用DbContext,它可以工作:
code> public class proj1dbContext:DbContext
{
public proj1dbContext():base(name = connStr)
...
似乎这是一个类似的问题
帮助EF代码第一个连接字符串
但不幸的是,它并没有给出一个明确的答案,为什么实例化ObjectContext会抛出该错误。
任何帮助都是可以接受的。谢谢!
ObjectContext
采用EF连接字符串(带元数据
和提供者连接字符串
关键字),而不是提供者特定的连接字符串。
您不能使用ObjectContext与Code-First; ObjectContext需要元数据XML文件。
I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext.
This is my connection string:
<add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" />
and this is the code that tries to create an instance of the ObjectContext:
var connectionString = ConfigurationManager
.ConnectionStrings["connStr"]
.ConnectionString;
ObjectContext _context = new ObjectContext(connectionString);
The last line throws System.ArgumentException: Keyword not supported: 'data source'.
I am using Visual Studio 2012 for Web and targeting .NET Framework 4.5. I have LocalDB installed on my machine.
If I use DbContext instead it works:
public class proj1dbContext: DbContext
{
public proj1dbContext() : base("name=connStr")
...
It seems that this is a similar question Help with EF Code first connection string but unfortunately it does not give a definitive answer to why instantiating ObjectContext throws that error.
Any help is appreaciated. Thanks!
ObjectContext
takes an EF connection string (with Metadata
and Provider Connection String
keywords), not a provider-specific connection string.
You can't use ObjectContext with Code-First; ObjectContext requires the metadata XML files.
这篇关于关键字不支持:'数据源':EF代码首先使用ObjectContext和LocalDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!