简体   繁体   English

LINQ-to-SQL-C#-OnValidate()

[英]LINQ-to-SQL - C# - OnValidate()

I have a system with 3 layers and I am using LINQ-to-SQL to persist. 我有一个包含3层的系统,并且正在使用LINQ-to-SQL进行持久化。

I want to validate if the description field of my object is empty. 我想验证我的对象的描述字段是否为空。 I am using partial classes and the method OnValidate() . 我正在使用部分类和方法OnValidate() If the field is empty, it throws an exception. 如果该字段为空,则抛出异常。

Is this correct? 这个对吗?

What do I do after the exception to not close the form, letting the user continue working? 发生异常后如何不关闭表单,让用户继续工作,我该怎么办?

Thank you very much 非常感谢你

Sorry for my poor english 对不起,我英语不好

How you handle it exactly depends on your what the application is (eg ASP.Net, MVC, WebForms), but your exception should return a list of the validation errors so that your view can handle them gracefully, eg display them on the form. 您如何处理它完全取决于您的应用程序是什么(例如ASP.Net,MVC,WebForms),但是您的异常应返回一个验证错误列表,以便您的视图可以优雅地处理它们,例如在表单上显示它们。

There are some good frameworks for handling this, such as XVal if it is a web application. 有一些很好的框架可以处理此问题,例如XVal(如果它是Web应用程序)。

查看此链接 ,其中显示了有关如何重写onValidate()的许多技巧和其他linq技巧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM