关键字不支持:“数据源”:EF code-首先使用的ObjectContext和的LocalDB [英] Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB

查看:1491
本文介绍了关键字不支持:“数据源”:EF code-首先使用的ObjectContext和的LocalDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个关键字,不支持错误当我尝试连接到使用ObjectContext的一个的LocalDB数据库。

I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext.

这是我的连接字符串:

<add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" />

这就是code,它试图创建的ObjectContext的实例:

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);

最后一行抛出System.ArgumentException:不支持关键字:数据源

The last line throws System.ArgumentException: Keyword not supported: 'data source'.

我使用Visual Studio 2012的Web和定位的.NET Framework 4.5。我已经安装的LocalDB我的机器上。

I am using Visual Studio 2012 for Web and targeting .NET Framework 4.5. I have LocalDB installed on my machine.

如果我用的DbContext,而不是它的工作原理:

If I use DbContext instead it works:

public class proj1dbContext: DbContext
{
    public proj1dbContext() : base("name=connStr")
    ...

看来,这是一个类似的问题
帮助与EF code首先连接字符串
但遗憾的是它并没有给一个明确的答案,为什么实例的ObjectContext抛出错误。

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.

任何帮助是paciated AP $ P $。谢谢!

Any help is appreaciated. Thanks!

推荐答案

的ObjectContext 需要一个EF连接字符串(使用提供连接字符串关键字),而不是一个特定的连接字符串。

ObjectContext takes an EF connection string (with Metadata and Provider Connection String keywords), not a provider-specific connection string.

您不能用code首先使用ObjectContext的; ObjectContext的需要元数据的XML文件。

You can't use ObjectContext with Code-First; ObjectContext requires the metadata XML files.

这篇关于关键字不支持:“数据源”:EF code-首先使用的ObjectContext和的LocalDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆