实体框架恢复更改
我正在开发一个 Web 表单,其中包含一个包含 4 个步骤的向导:
在每个步骤中,我都会创建从数据库生成的新实体。
问题是,作为向导,用户可以更改将生成要存储的值的控件的属性。
因此,我需要释放创建的实体对象或将该实体值返回到存储在数据库中的原始行。
我该怎么办呢。
如果我将每个创建的实体对象设置为空,它应该起作用吗?
顺便说一句,这就是我正在做的事情:
entities = new Entities();
...
Client client = new Client();
client.name = tbxName.text
...
entities.SaveChanges();
entities.Connection.Close();
因此,如果此代码在由 3 个部分组成的向导的第二个向导部分上执行,并且一旦客户端创建运行多次,我就会前后遍历此集合,所以这是我的问题。
那么我怎样才能取消创建它:-P
谢谢!
I am develloping a web form that has a wizard with 4 steps:
On each step I'me creating new entities generated from a database.
The problem is that being a wizzard, the user can change the properties of the controls that will originate the values to be stored.
So I need to release the created entity objects or return that entity values to the original rows stored on the database.
How can I do this.
Should'n it work if I set each created entity object to null?
By the way this is how I'm doing it:
entities = new Entities();
...
Client client = new Client();
client.name = tbxName.text
...
entities.SaveChanges();
entities.Connection.Close();
So If this code is executed on the 2nd wizard part of a wizard of 3 parts and I go back and fowrward through this set more the once the client creating runs more than once, so there's my problem.
So how can I unCreate it :-P
Thannks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在构建向导,则必须将其作为单个操作进行管理。这意味着您必须将构建的实体图存储在会话中,并且仅在整个向导完成并确认后才保存。您的步骤逻辑还必须检查实体图中是否已存在相关数据并使用它们而不是创建新数据。
If you are building wizard you must manage it as single operation. It means that you have to store built entity graph in the session and save it only if whole wizard is completed and confirmed. Your step logic also must check if related data are already present in the entity graph and use them instead of creating new one.
如果您使用实体框架,为什么不实现工作单元模式呢?向导的每个部分都会构建 UoW,“最后一步”会提交工作单元。
有一篇文章名为“工作单元模式和持久性无知”几年前的 MSDN 杂志解释了这个概念。
If your using Entity Framework, why not implement the Unit Of Work pattern? Each part of your wizard builds the UoW and the "final step" commits the unit of work.
There was an article called "The Unit Of Work Pattern And Persistence Ignorance" in MSDN magazine a few years ago that explains the concept.
我是这样做的:
1- 创建一个可以管理 Session 变量的地方:
2- 我将 ObjectContext 保存在 Session 中,因此我创建一个属性来在提到的类中管理它:
3- 初始化向导启动并在其结束时处理它:
4- 要将向导结果保存到数据库,您可以调用:
5- 要重置向导,只需调用 EndWizard 然后调用 StartWizard 。
我想您知道如何在 ObjectContext 中管理您的 ObjectEntity 对象,这样您就可以自己从这里继续..
This is the way I do it:
1- Create a place where you can manage your Session variables :
2- I save my ObjectContext in the Session so I create a property to manage it in the mentioned class :
3- Initialize the ObjectContext on the wizard's start and dispose it on its end:
4- To save wizard result to the database you can call:
5- To reset wizard simply call EndWizard then StartWizard .
I guess you know how to manage your ObjectEntity objects and in the ObjectContext so you can continue from here by your self ..