使用断开连接的实体Working with disconnected entities

在基于实体框架的应用程序中,上下文类负责检测应用于已跟踪实体的更改。In an Entity Framework-based application, a context class is responsible for detecting changes applied to tracked entities. 调用 SaveChanges 方法会将上下文跟踪的更改保存到数据库。Calling the SaveChanges method persists the changes tracked by the context to the database. 使用 n 层应用程序时,实体对象通常会在从上下文断开连接时发生变动,且必须决定如何跟踪更改并向上下文报告这些更改。When working with n-tier applications, entity objects are usually modified while disconnected from the context, and you must decide how to track changes and report those changes back to the context. 本主题讨论使用实体框架和断开连接的实体时不同的可用选项。This topic discusses different options that are available when using Entity Framework with disconnected entities.

Web 服务框架Web service frameworks

Web 服务技术通常支持可用于在各个断开连接的对象上保存更改的模式。Web services technologies typically support patterns that can be used to persist changes on individual disconnected objects. 例如,通过 ASP.NET Web API 可编写控制器操作,这些操作可以包括对 EF 的调用,以保存对数据库上的对象所做的更改。For example, ASP.NET Web API allows you to code controller actions that can include calls to EF to persist changes made to an object on a database. 实际上,Visual Studio 中的 Web API 工具可以轻松地从实体框架 6 模型构建 Web API 控制器。In fact, the Web API tooling in Visual Studio makes it easy to scaffold a Web API controller from your Entity Framework 6 model. 有关详细信息,请参阅一起使用 Web API 和实体框架 6For more information, see using Web API with Entity Framework 6.

以前,还有其他一些 Web 服务技术提供与实体框架的集成,例如 WCF 数据服务RIA 服务Historically, there have been several other Web services technologies that offered integration with Entity Framework, like WCF Data Services and RIA Services.

低级别 EF APILow-level EF APIs

如果不想使用现有的 n 层解决方案,或者想要自定义 Web API 服务中控制器操作内发生的内容,则可通过实体框架提供的 API 应用在断开连接的层上所做的更改。If you don't want to use an existing n-tier solution, or if you want to customize what happens inside a controller action in a Web API services, Entity Framework provides APIs that allow you to apply changes made on a disconnected tier. 有关详细信息,请参阅添加、附加和实体状态For more information, see Add, Attach, and entity state.

自跟踪实体Self-Tracking Entities

在从 EF 上下文断开连接时,跟踪实体的任意图上的更改是一个难题。Tracking changes on arbitrary graphs of entities while disconnected from the EF context is a hard problem. 可采用自跟踪实体代码生成模板来尝试解决。One of the attempts to solve it was the Self-Tracking Entities code generation template. 此模板生成实体类作为实体本身的状态,该类包含可跟踪断开连接的层上所做更改的逻辑。This template generates entity classes that contain logic to track changes made on a disconnected tier as state in the entities themselves. 还会生成一组扩展方法,将这些更改应用于上下文。A set of extension methods is also generated to apply those changes to a context.

此模板可与使用 EF 设计器创建的模型一起使用,但不能与 Code First 模型一起使用。This template can be used with models created using the EF Designer, but can not be used with Code First models. 有关详细信息,请参阅自跟踪实体For more information, see Self-Tracking Entities.

重要

我们不再建议使用自跟踪实体模板。We no longer recommend using the self-tracking-entities template. 它将仅继续用于支持现有应用程序。It will only continue to be available to support existing applications. 如果应用程序需要使用断开连接的实体图,请考虑其他替代方案,例如可跟踪实体,它与自跟踪实体类似,社区在更积极地开发这种技术,或使用低级别更改跟踪 API 编写自定义代码。If your application requires working with disconnected graphs of entities, consider other alternatives such as Trackable Entities, which is a technology similar to Self-Tracking-Entities that is more actively developed by the community, or writing custom code using the low-level change tracking APIs.