的SubmitChanges之前验证LINQ2SQL() [英] Validate Linq2Sql before SubmitChanges()

查看:146
本文介绍了的SubmitChanges之前验证LINQ2SQL()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,如果/如何的的验证LINQ2SQL中的变化在数据环境中调用的SubmitChanges()。我的情况是,我创建了一个背景下,执行多个操作,并添加许多插入与其他处理任务,然后回滚,如果提交失败。

Can anyone tell me if/how you can validate the changes in a data context in Linq2Sql before calling SubmitChanges(). The situation I have is that I create a context, perform multiple operations and add many inserts alongside other processing tasks and then rollback if the submit fails.

我倒是preFER做的是做出某种验证()某些任务后,调用完成,这样我可以在提交整个作业之前处理这件事的。

What I'd prefer to do is make some kind of "Validate()" call after certain tasks are done so that I can handle it before submitting the entire job.

推荐答案

要得到所有的变化在数据环境中,你可以调用

To get all the changes in a data context you can call

ChangeSet changes = dataContext.GetChangeSet();

// An IList<Object>
changes.Deletes;
changes.Inserts;
changes.Updates;

我有什么是每个值对象有一个validate方法。我用attibutes定义不同种类的验证。我做手工的原因是因为我有一个数字,也许在数据库和code一个int,1002的值可能是无效的,如果我保存的年龄。所以,我可以给一个范围值,等等。 。

What I have is each value object has a validate method. I use attibutes to define different sorts of validation. The reason I do it manually is because an I have a number that maybe an int in the database and the code, a value of 1002 could be invalid if I saving a age. So I can give a range of values, etc. . .

如果你的每个值对象的继承基础对象时,它让我们很容易中将其中。假设你有你的基类中的验证方法。

If each of your value objects inherit from a base object it makes iterating them easier. Assuming you have on your base class a Validate method.

我要指出这个工作,你将不得不要么编辑生成的code,或推出自己的值对象。我通常滚,因为我是如何用它们来验证自己的。

I would point out for this to work, you are going to have to either edit the generated code, or roll your own value objects. I typically roll my own because of how I use them for validation.

这篇关于的SubmitChanges之前验证LINQ2SQL()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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