在 EF 4.1 中使用 DbContext 和数据库优先

发布于 2025-01-05 22:25:14 字数 376 浏览 0 评论 0原文

我已经开始开发一个新项目,并从 LinqToSQL 切换到 EF 4.1 作为我的 ORM。

我已经设置了一个可以使用的数据库,因此我将采用数据库优先方法。默认情况下,EF 生成一个扩展ObjectContext 的上下文。我想知道用 DbContext 替换它是否是一个好方法。

大多数可用示例仅涉及 Code First 和 DbContext,但 DBContext 也可以与 Database First 一起使用。使用 DBContext 有什么好处吗?据我所知,DBContext 是 ObjectContext 的简化版本,使其更易于使用。还有其他优点或缺点吗?

I have started working on a new project and am switching from LinqToSQL to EF 4.1 as my ORM.

I already have a database set up to work with and so am going with the database first approach. By default the EF generates a context which extends ObjectContext. I wanted to know if a good approach would be to replace it with DbContext.

Most of the available examples deal with only Code First and DbContextbut DBContext can be used with Database First too. Are there any advantages I get by using the DBContext? From what I have read the DBContext is a simplified version of the ObjectContext and makes it easier to work with. Are there any other advantages or disadvantages?

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

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

发布评论

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

评论(2

寂寞陪衬 2025-01-12 22:25:14

您不会手动替换任何内容。您将需要 VS Gallery 中提供的 DbContext T4 生成器。不要触摸自动生成的文件 - 每次修改 EDMX 文件时,您的更改都会丢失。

我回答了类似问题去年。现在我的答案主要是——对于新用户来说,DbContext API 可能更好。 DbContext API 在用法和功能方面都得到了简化,但您仍然可以从 DbContext 获取 ObjectContext 并使用仅在 ObjectContext API 中可用的功能。另一方面,DbContext API 具有一些额外的性能影响和额外的错误层。在简单的项目中,您可能不会发现 DbContext API 的任何缺点 - 您不会看到性能影响,您不会使用仅在 ObjectContext 中可用的角落功能,并且不会受到偶尔错误的影响。

自 DbContext API 发布以来,我们收集了很多信息和博客文章,因此您不必担心找不到 API 的描述。此外,ADO.NET 团队现在使用 DbContext API 作为他们的旗舰。

我不是 DbContext API 的忠实粉丝,但我的观点与它的功能无关,而是与它的存在有关 - 不需要有两个 API 和 ADO.NET 团队的拆分开发能力来维护和修复两个 API 做同样的事情。这仅意味着实现真正新功能的能力较少。

You will not replace anything manually. You will need DbContext T4 Generator available at VS Gallery. Don't touch your autogenerated files - your changes will be lost every time you modify EDMX file.

I answered similar question last year. Now my answer is mostly - for new users DbContext API is probably better. DbContext API is simplified - both in terms of usage and features but you can still get ObjectContext from DbContext and use features available only in ObjectContext API. On the other hand DbContext API has some additional performance impact and additional layer of bugs. In simple project you will probably not find any disadvantage in DbContext API - you will not see performance impact, you will not use corner features available only in ObjectContext and you will not be affected by occasional bugs.

A lot of information and blog posts was collected since DbContext API was released so you don't have to be afraid that you will not find description of the API. Also ADO.NET team now uses DbContext API as their flag ship.

I'm not a big fan of DbContext API but my opinion is not related to its functionality but to its existence - there is no need to have two APIs and split development capacity of ADO.NET team to maintain and fix two APIs doing the same. It only means that there is less capacity for implementation of really new features.

掀纱窥君容 2025-01-12 22:25:14

我现在将它与 Oracle 一起使用,作为现有应用程序的附加组件。拉迪斯拉夫提到的简化对我来说在这个项目上很有效,因为我的时间和资源都有限。只要您坚持简单的 CRUD 操作和少于 150 个表,我就没有发现任何问题。

您仍然可以使用元数据注释来提供基本的验证和本地化,并且有足够的文档,但您在 Microsoft 官方网站上找不到太多文档。

I'm using it now with Oracle on an add on to an existing application. The simplification that Ladislav refers to works well for me on this project as I am short on time and resources. I have not found any gotchas as long as you stick to simple CRUD operations and less than ~150 tables.

You can still use metadata annotations to provide basic validation and localization and there is enough documentation out there but you won't find much on official Microsoft sites.

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