如何使用 silverlight wcf ria 服务防止表中出现重复条目

发布于 2024-12-11 04:04:36 字数 393 浏览 0 评论 0 原文

我正在使用下面的链接开发一个示例应用程序。

http://code.msdn.microsoft.com/Getting-Started-WCF -RIA-1469cbe2

正如链接中提到的,它是一个使用 wcf ria 服务的 silverlight 业务应用程序。

在示例中,在添加新员工时,如果员工“职称”字段在数据库中是唯一的,并且用户尝试输入重复的职称,我该如何防止呢?我的意思是,如果输入两个相同的“标题”,它应该正确验证。在当前情况下我该如何做到这一点?我正在使用子窗口添加新员工...

有人可以帮忙吗?

i am developing a sample app using the link below.

http://code.msdn.microsoft.com/Getting-Started-WCF-RIA-1469cbe2.

Its a silverlight business application using wcf ria services as mentioned in the link.

in the sample, while adding a new employee,if the employee 'title' field is unique in database and user try to enter duplicate title,how can i prevent it? i mean if one enters two same 'title' it should validate properly. How can i do this in the current scenario? i am adding new employee using a child window...

can any one help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-12-18 04:04:36

您可以使用一个 CustomValidation 来解决您的问题,我认为这是最好的方法,因为您可能会或可能不会在客户端加载所有记录,并且如果您没有加载所有员工,您需要去数据库进行检查如果有一个标题被输入...

基本上会像这样:

首先创建将负责员工验证的类

public class EmployeeValidationRules
{
}

,然后创建实际执行验证的方法:

public static ValidationResult ValidateTitle(string title, ValidationContext context)
{
    AdventureWorks_DataEntities objectContext = new AdventureWorks_DataEntities();

    Employee employee = context.ObjectInstance as Employee ;
    if (objectContext.Emplyees.Any(e => e.Title == employee.Title))
    {
        return new ValidationResult("Title Allready Exists", new string[] { "Title" });
    }

    return ValidationResult.Success;
}

在由域服务生成的元数据上雇员类搜索 title 属性,您必须添加此标签:

[CustomValidation(typeof(EmployeeValidationRules), "ValidateTitle")]     
public string Title{ get; set; } 

注意:所有这些都必须在服务器端完成...

您可以在此处找到有关 WCF 验证的更多信息:

http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx

这是 Jeff Handley 写的另一篇关于 WCF 验证的非常好的文章:

http://jeffhandley.com/archive/2010/09/25/RiaServicesCustomValidationMethods.aspx

You can solve your problem using one CustomValidation, and i think its the best way since you may or may not have all records loaded on the client side, and in the case you dont have all employes loaded you need to go to the DB to check if there is one with the title beeing inputed...

Basically it would go like this:

First Create the Class that's going to be responsible for the Employees Validation

public class EmployeeValidationRules
{
}

then the method that will actually do the Validation:

public static ValidationResult ValidateTitle(string title, ValidationContext context)
{
    AdventureWorks_DataEntities objectContext = new AdventureWorks_DataEntities();

    Employee employee = context.ObjectInstance as Employee ;
    if (objectContext.Emplyees.Any(e => e.Title == employee.Title))
    {
        return new ValidationResult("Title Allready Exists", new string[] { "Title" });
    }

    return ValidationResult.Success;
}

On the metadata generated by your domainservices in the Employee class search for the title property and you have to add this tag:

[CustomValidation(typeof(EmployeeValidationRules), "ValidateTitle")]     
public string Title{ get; set; } 

Note: All this must be done on the server side...

You can find more information about WCF Validations here:

http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx

Here is another very well written article by Jeff Handley on WCF validations:

http://jeffhandley.com/archive/2010/09/25/RiaServicesCustomValidationMethods.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文