从实体框架连接字符串创建 DataContext?

发布于 2024-11-07 02:25:00 字数 1001 浏览 0 评论 0原文

我试图在我的代码中进行此调用:

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

这是我的连接字符串:

<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”未映射为表。

我在这里做错了什么?

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="Data Source=STEVEN-PC;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
  <add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>

I get an error when creating the DataContext: Keyword not supported: 'metadata'.

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.

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

垂暮老矣 2024-11-14 02:25:00

您正在混合和匹配 LINQ-to-SQLLINQ-to-Entites;两者不兼容。

当您使用 LINQ-to-Entities 创建实体模型时,它会创建一个派生自 ObjectContext 它将具有 IQueryable实现,您将用于查询的基础。此 ObjectContext 还将具有采用适当的元数据将实体模型映射到数据库的构造函数;这就是实体框架连接字符串需要元数据引用的原因。

当您尝试使用 LINQ-to-SQL 时,可以将其传递到 DataContext 类(或派生类)。 DataContext 处理对象到数据库的映射的方式与实体框架不同;它依赖于模型上的属性来映射到表/列(使用 TableAttributeColumnAttribute 属性分别)。当您使用实体框架创建实体时,这些属性不存在。

注意:您可以将 XML 映射文件(与实体框架中使用的类型不同)与 LINQ-to-SQL 结合使用,但并不常用。

也就是说,最简单的方法是选择一种技术堆栈(LINQ-to-SQL 或 LINQ-to-Entities)并坚持使用。

You are mixing and matching LINQ-to-SQL and LINQ-to-Entites; the two are not compatible.

When you create entity models with LINQ-to-Entities, it creates an object derived from ObjectContext which would have the IQueryable<T> implementations that you would use for the base of your query. This ObjectContext also will have constructors that take the appropriate metadata to map the entity models to the database; this is why Entity Framework connection strings require Metadata references.

When you try and use LINQ-to-SQL, you can pass it a regular database connection to the DataContext class (or a derived class). The DataContext handles the mapping of your objects to the database differently than the Entity Framework; it relies on attributes on the models to map to tables/columns (using the TableAttribute and ColumnAttribute attributes respectively). These attributes are not present when you created entities using the Entity Framework.

Note: You can use XML mapping files (a different sort than what is used in the Entity Framework) with LINQ-to-SQL, but it's not commonly used.

That said, the easiest approach would be to choose one technology stack (LINQ-to-SQL or LINQ-to-Entities) and stick with that.

酒绊 2024-11-14 02:25:00

如果您想使用实体框架,您应该使用 ObjectContext,而不是 DataContext,因为这是 Linq-To-Sql 的基类。

创建 ADO.NET 实体数据模型 时,Visual Studio 会生成(在从数据库向导完成生成模型或使用设计器后)一个从 ObjectContext 派生的类,该类具有默认连接字符串(您在向导中选择) 。
此处 您可以看到 ADO.NET 团队提供的关于如何开始使用 EF 的精彩教程。

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

如果您想提供自己的与类的连接,您可以使用 EntityConnectionStringBuilder

This 是一个如何使用 EntityConnectionStringBuilder 的示例,来自 MSDN

编辑:我错误地写了有关 DataContext 的文章,就好像它是设计器生成代码的 EF 基类一样。正如 casperOne 所说,它是 Linq-To-Sql 类的基类。

更改了我的答案以反映他的评论

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

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.

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.

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

This is an example how to use EntityConnectionStringBuilder it from MSDN

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.

Changed my answer to reflect his comment

救赎№ 2024-11-14 02:25:00

以防万一,这就是我所做的。我在 Web.config 中没有连接,因为我需要 DropDownList 来选择连接。

string connDev = @"metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=""Server=MyDevServer;Database=MyDB;Integrated Security=True""";

EntityConnection ec = new EntityConnection(connDev);

MyDBContext db = new MyDBContext(ec);

var people = db.People.ToList();

Just in case, this is what I did. I don't have connections in the Web.config since I need a DropDownList to select the connection.

string connDev = @"metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=""Server=MyDevServer;Database=MyDB;Integrated Security=True""";

EntityConnection ec = new EntityConnection(connDev);

MyDBContext db = new MyDBContext(ec);

var people = db.People.ToList();
今天小雨转甜 2024-11-14 02:25:00

连接字符串已在 EntityModel 中定义,因此您可以尝试使用默认连接,如下所示:

using (context = new DataContext())
{
    var myTable = context.GetTable<MyApp.Entities.Employee>();
}

您还可以尝试以下操作:

MyApp.Entities.Employee myTable = context.GetTable<MyApp.Entities.Employee>();

EDIT: GetTable() 将返回类型 T,因此上面的语法将是正确的。

如果要覆盖连接字符串,请使用 web.config 文件 (MyDB) 中的第二个连接字符串

The connectionstring is already defined in the EntityModel, so you can try to use the default connection as follows:

using (context = new DataContext())
{
    var myTable = context.GetTable<MyApp.Entities.Employee>();
}

You can also try the following:

MyApp.Entities.Employee myTable = context.GetTable<MyApp.Entities.Employee>();

EDIT: GetTable<T>() will return type T, so the syntax above will be correct.

If you want to override the connection string, use the second connection string from your web.config file (MyDB)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文