Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB

旧时模样 提交于 2019-12-06 09:11:16

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/14087430/keyword-not-supported-data-source-ef-code-first-using-objectcontext-and-loc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!