从实体框架的连接字符串创建的DataContext? [英] Create DataContext from Entity Framework connection string?

查看:156
本文介绍了从实体框架的连接字符串创建的DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的code这个调用:

I'm trying to make this call in my code:

string conn = ConfigurationManager.ConnectionStrings["MyDBEntities"].ConnectionString;
DataContext context = new DataContext(conn);
Table<MyApp.Entities.Employee> myTable = context.GetTable<MyApp.Entities.Employee>();

下面是我的连接字符串:

Here's my connection strings:

<connectionStrings>
  <add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=STEVEN-PC;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  <add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>

在创建的DataContext时,我得到一个错误:不支持关键字:元数据

如果我使用第二个连接字符串,我试图让该表时,得到一个错误。
类型'MyApp.Entities.Employee'不是映射为一个表。

If I use the second connection string, I get an error when trying to get the table: Server Error in '/' Application. The type 'MyApp.Entities.Employee' is not mapped as a Table.

我在做什么错在这里?

推荐答案

如果你想使用实体框架,你应该使用ObjectContext的,而不是DataContext的,因为这是从LINQ到SQL。

If you want to use Entity Framework you should use ObjectContext, not DataContext as this is a base class from Linq-To-Sql.

在创建ADO.NET实体数据模型时,Visual Studio生成(您完成之后,从数据库向导生成的模型或使用设计器),它有一个默认的连接字符串是从ObjectContext的派生的类(您选择在巫师)。
 <一href=\"http://blogs.msdn.com/b/adonet/archive/2010/07/19/absolue-beginners-guide-to-entity-framework.aspx\"相对=nofollow>在这里你可以看到ADO.NET队一个很好的教程如何开始使用EF。

When you create ADO.NET Entity Data Model , Visual Studio generates (after you complete a generate model from database wizard or use the designer), a class that is derived from ObjectContext that has a default connection string (that you choose at the wizard). Here you can see a nice tutorial from ADO.NET team how to start using the EF.

您不应该直接使用ObjectContext的,至少在没有手动创建元数据文件,并在连接字符串中指向它们(没见过直接使用DataContext类,因此,如果我错了,有人纠正我),因为我上面提到的向导创建映射数据的种种 - 到SQL表/视图/其他的东西映射到实体类

You are not supposed to use ObjectContext directly, at least not without manually creating the metadata files and pointing to them in your connection string (Never seen DataContext class being used directly, so if I'm wrong someone correct me) , as the wizard I mentioned above creates all sorts of mapping data - to map SQL tables/views/other stuff to Entity classes.

如果你想提供自己的连接类可以通过编程方式与做<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.aspx\"相对=nofollow> EntityConnectionStringBuilder 。

if you want to supply your own connection to the class you can do it programmatically with EntityConnectionStringBuilder.

是一个例子,如何使用它EntityConnectionStringBuilder从MSDN

This is an example how to use EntityConnectionStringBuilder it from MSDN

编辑:我错误地写了一篇关于DataContext的,就好像它是设计器生成code EF基类。这是因为的 casperOne 的规定对LINQ到SQL类的基类。

Edit : I mistakenly wrote about DataContext as if it was EF base class for designer generated code. It is as casperOne stated a base class for Linq-To-Sql classes.

改变了我的答案,以反映他的意见

这篇关于从实体框架的连接字符串创建的DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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