连接模型和EF分离模型 [英] connected model and disconnected model in EF

查看:264
本文介绍了连接模型和EF分离模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑了很多关于连接的模型和实体框架断开连接。

I'm confused a lot about connected model and disconnected in entity framework .

我用的是传统的ADO.net(的DataReader 的连接模式和的DataAdapter 断开连接模型)
而我知道,我用的时候我有很多用户需要更新或插入在一起,并在少数情况下,断开连接的模型,当我需要将数据发送到其他进程就在内存中的数据的一些操作,并送他们回连接模式到数据库。

I was using traditional ADO.net (DataReader for connected model and DataAdapter for disconnected model) and all I know that I use connected model when I have many users need to update or insert together and the disconnected model in a few circumstances when I need to send the data to other process make some operations on the data in memory and send them back to the db .

现在我读到连接模式和断开连接的模型EF一些文章,我很困惑,为什么我要高度明确的实体在断开连接的模型的背景?
我读也,在网络的默认行为是断开连接的模型,并在WPF连接模型!

Now I read some articles about connected model and disconnected model in EF and I'm confused why should I attach explicitly the entities to the context in disconnected model ? I had read also that the default behavior in web is disconnected model and in WPF is connected model !


  • 有人能以简单的方式与解释现实生活中的比喻
    什么是两种模式之间的区别?

  • 我们如何能够在与EF简单的例子,同时处理模式?

  • 是否有应用程序的类型之间的关系(Web表单
    ,MVC,WPF,WCF)和EF使用专用模式?

  • 当使用连接的模型,以及何时使用断开连接的模型(使用EF)?

推荐答案

的ADO.NET框架支持数据访问架构的两种模式:

A Background

The ADO.NET Framework supports two models of Data Access Architecture:


  1. 面向连接

  2. 断开连接

在面向数据访问架构连接的应用程序对数据源的连接,然后与它交互通过SQL请求的使用相同的连接的(例如,开放式连接必须保持你的应用程序和数据源,即使它不使用任何数据库操作)之间。

In Connection Oriented Data Access Architecture the application makes a connection to the Data Source and then interact with it through SQL requests using the same connection (e.g. an open connection must be maintained between your application and the Data Source even when it is not using any Database Operations).

连接的架构是,当你不断做出旅行对数据库的CRUD(创建,读取,更新和删除),你希望做的操作。这将创建更多的流量到数据库,但通常要快得多,你应该做的更小的交易。

Connected architecture is when you constantly make trips to the database for any CRUD (Create, Read, Update and Delete) operation you wish to do. This creates more traffic to the database but is normally much faster as you should be doing smaller transactions.

它是建立在类连接命令的DataReader 交易

在这里输入的形象描述

在断开连接的数据访问架构的ADO.net在内存中的数据存储,可容纳在同一时间(他们之前获取)。

In Disconnected Data Access Architecture the ADO.net uses in-memory data store that can hold multiple tables at the same time (they're fetched before).

已断开连接的架构是从数据库中检索创下了一个纪录并存储给你做许多CRUD(创建,读取,更新和删除),对内存中的数据操作能力的方法,那么可以重新同步与数据库重新连接时。

Disconnected architecture is a method of retrieving a record set from the database and storing it giving you the ability to do many CRUD (Create, Read, Update and Delete) operations on the data in memory, then it can be re-synchronized with the database when reconnecting.

它是建立在类连接的DataAdapter CommandBuilder的的DataSet 数据视图

It's Built on classes Connection, DataAdapter, CommandBuilder, DataSet and DataView.

在这里输入的形象描述


  • 的DataReader 连接架构的,因为它使
    连接打开,直到所有行被提取一个接一个。

  • 的DataSet 终止的已断开连接架构的,因为所有的记录都
    同时带来的,也没有必要保持连接。

  • 的DataAdapter 充当连接和沟通的桥梁
    断开连接的对象。它管理数据源和之间的连接
    数据集从数据源中的数据填充到数据集

  • DataReader is Connected Architecture since it keeps the connection open until all rows are fetched one by one.
  • DataSet is Disconnected Architecture since all the records are brought at once and there is no need to keep the connection alive.
  • DataAdapter acts as a bridge between the Connected and Disconnected Objects. It manages connections between Data Source and Dataset by fill the data from Data Source to the Dataset.

  • 面向连接

  • 我们用的DataReader 对象从数据库中读取数据

  • 其方法提供更快的性能

  • 它可以容纳一个表中的数据

  • 这是只读,不能更新数据

  • connection oriented
  • we read data from a database by using a DataReader object
  • Its methods provide faster performance
  • It can hold the data of single table
  • It's read only, we can't update the data

  • 它断开面向连接的。

  • 我们用的DataSet 对象从数据库中读取数据。

  • 它获得速度和性能低。

  • 它可以容纳多个数据表。

  • 我们可以执行所有选项像更新,插入,删除等。

  • It's disconnected connection oriented.
  • we read data from a database by using a DataSet object.
  • It get low in speed and performance.
  • It can hold multiple tables of data.
  • we can perform all option as like update, insert, delete etc.

现在我读到连接模式和断开连接的模型的一些文章
  在EF,我很困惑,为什么我要重视明确的实体
  在断开连接的模型的背景?我也读到默认
  在网络行为被断开模型和WPF连接模型!

Now I read some articles about connected model and disconnected model in EF and I'm confused why should I attach explicitly the entities to the context in disconnected model? I had read also that the default behavior in web is disconnected model and in WPF is connected model !

Web应用程序可以连接或断开,并且,其实ASP.NET应用程序都断开,由于ADO.NET断开连接模式。断开模式由于实现简单,更方便地解决越来越受欢迎。与ASP.NET应用程序良好的设计将尽快关闭所有的数据库连接数据操作完成后,无论它是否是每月15命中或每秒15命中。

Web application could be connected or disconnected, and, in fact ASP.NET applications are disconnected, due to ADO.NET disconnected model. Disconnected model is getting more popular due to simple implementation and easier troubleshooting. Good design with ASP.NET application would close all the database connections as soon as data manipulation is complete regardless if it is 15 hit per month or 15 hits per second.

有人能以简单的方式与解释现实生活中的比喻
  什么是两种模式之间的区别?

Could someone explain in easy manner with an an analogy of real life what's the difference between the two models?

是的,假设你有一些重要的提示,告诉/意识到一个朋友。 断开连接意味着你期待已久见他的方式或者你花时间获得更多的技巧来再说。 连接是这样的,当你和他一起住或有一个在线/实时沟通,他希望每个时间告诉每一个提示。

Yes, Suppose you have some important tips to tell/aware a friend. Disconnected means the way you've awaited to see him or you're spending time to obtain more tips to say. Connected is the way when you live with him or having an online/RealTime communication to him for telling every tip each time you want.

我们如何能够在与EF简单的例子,同时处理模式?

How we could handle both models in EF with simple example?

EF使用的已断开连接模式。因为你的数据工作,使所需的更改,然后您执行的SaveChanges :)

EF uses the Disconnected model. since you work with data and make desired changes and then you perform the SaveChanges :)

有(Web表单,MVC,WPF应用程序的类型之间的关系,
  WCF)和EF使用专用模式?

Is there a relationship between the type of app (web form , MVC, WPF, WCF) and the dedicated model used in the EF?

它基于应用程序逻辑。实时应用需要连接,因为他们需要的导通时间的传播和更新,而不是另一种类型的应用程序

It's based on application logic. RealTime applications need to be connected as they need on-time propagation and updates rather than another application types.

在使用连接模式以及何时使用断开连接的模型(用
  EF)?

When to use connected model and when to use disconnected model (using EF) ?

我已经回答了这一点。只是我想说通过保持连接的开放只是时间的最低期限,ADO.NET节省系统资源和数据库的安全性最高,也有对系统性能的影响较小。它是基于应用程序的策略/类型,你可以让自己一个很好的决定。

I've answered this. Just I would like to say By keeping connections open for only a minimum period of time, ADO.NET conserves system resources and provides maximum security for databases and also has less impact on system performance. It's based on your application strategy/type, you can make a good decision yourself.

更新

但是,如果我是在一个断开连接的模型,你能告诉我怎么能EF
  处理多个业务从许多用户(插入,更新,删除)
  在同一时间?

but if i was in a disconnected model, Could you tell me how the EF can handle multiple operations (INSERTs,UPDATEs,DELETEs) from many users at the same time?

看看 ObjectStateManager 它负责一切对象在上下文跟踪有关。的如果某些用户执行并发更新同一条目的,默认情况下,实体框架实现了一个乐观的并发模型。这意味着,锁定不会保持在当数据查询和更新数据之间的数据源的数据。 实体框架,不检查并发保存对象对数据库的修改。对于可能遇到高度并发(如银行系统)的实体,我们建议实体定义在概念层的属性的 ConcurrencyMode =固定的属性,如下面的例子:

Take a look at ObjectStateManager which is responsible for everything related to object tracking in the context. If some users perform concurrent updates to the same entry, By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated. The Entity Framework saves object changes to the database without checking for concurrency. For entities that might experience a high degree of concurrency (like banking system), we recommend that the entity define a property in the conceptual layer with an attribute of ConcurrencyMode="fixed", as shown in the following example:

<Property Name="Status" Type="Byte" Nullable="false" ConcurrencyMode="Fixed" />

在使用这个属性,实体框架检查将更改保存到数据库之前,在数据库中的变化。任何冲突的变化会引起<一个href=\"https://msdn.microsoft.com/en-us/library/vstudio/system.data.optimisticconcurrencyexception(v=vs.100).aspx\"相对=nofollow> OptimisticConcurrencyException 当你定义使用存储过程进行更新数据源实体数据模型也可以发生。在这种情况下,当用于执行更新存储过程报告说零行更新的异常。欲了解更多信息,请访问保存更改和管理并发

When this attribute is used, the Entity Framework checks for changes in the database before saving changes to the database. Any conflicting changes will cause an OptimisticConcurrencyException which can also occur when you define an Entity Data Model that uses stored procedures to make updates to the data source. In this case, the exception is raised when the stored procedure that is used to perform updates reports that zero rows were updated. For more information visit Saving Changes and Managing Concurrency

这篇关于连接模型和EF分离模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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