LightSwitch 2011数据字段唯一性验证方案

开发 开发工具
我们将谈到的是LightSwitch 2011数据字段唯一性验证方案,这个方案其实不复杂。希望对大家有所帮助。

  LightSwitch 2011 数据字段唯一性验证方案

验证单表数据的某个字段不能输入重复值

  设置实体字段唯一索引

  如果不写代码,那么验证只会在用户提交[保存]数据后,会提示错误,很明显这样的用户体验并不好,因此还需要做以下步骤

  添加自定义验证

  1.   partial void UserName_Validate(EntityValidationResultsBuilder results)  
  2.   {  
  3.   // results.AddPropertyError("<错误消息>");  
  4.   bool duplicateExists = false 
  5.   switch (this.Details.EntityState)  
  6.   {  
  7.   case EntityState.Added:  
  8.   {  
  9.  //基于页面未提交数据的验证  
  10.   duplicateExists = (from item in DataWorkspace.ApplicationData.Details.GetChanges().AddedEntities.OfType<Employee>()  
  11.  where item.UserName == this.UserName && !string.IsNullOrEmpty(this.UserName)  
  12.   select item).Count() > 1 ? true : false 
  13.   //基于数据库的验证  
  14.   if (!duplicateExists)  
  15.   duplicateExists = (from Employee emp in DataWorkspace.ApplicationData.Employees.Cast<Employee>()  
  16.   where this.UserName != null &&  
  17.   string.Compare(emp.UserName, this.UserName.Trim(), StringComparison.InvariantCultureIgnoreCase) == 0  
  18.   select emp).Any();  
  19.   break 
  20.   }  
  21.   case EntityState.Modified:  
  22.   {  
  23.   duplicateExists = (from item in DataWorkspace.ApplicationData.Details.GetChanges().ModifiedEntities.OfType<Employee>()  
  24.   where item.UserName == this.UserName && !string.IsNullOrEmpty(this.UserName)  
  25.   select item).Count() > 1 ? true : false 
  26.   if (!duplicateExists)  
  27.   duplicateExists = (from Employee emp in DataWorkspace.ApplicationData.Employees.Cast<Employee>()  
  28.   where this.UserName != null &&  
  29.   string.Compare(emp.UserName, this.UserName.Trim(), StringComparison.InvariantCultureIgnoreCase) == 0  
  30.   select emp).Any();  
  31.   break 
  32.   }  
  33.  }  
  34.   if (duplicateExists)  
  35.   {  
  36.   results.AddPropertyError(string.Format("该用户[{0}]已经存在。", UserName));  
  37.   } 

  运行结果如下

原文链接:http://www.cnblogs.com/neozhu/archive/2011/10/19/2217221.html

【编辑推荐】

  1. 小试一下微软开发框架LightSwitch
  2. Visual Studio简化版推出 供非专业人员使用
  3. Visual Studio LightSwitch安装与配置详解
  4. 详解Visual Studio 2010辅助敏捷测试
  5. Visual Studio 2010中特殊表格的开发
责任编辑:彭凡 来源: 博客园
相关推荐

2021-05-20 08:16:57

数据库数据软删除数据

2022-01-27 11:02:04

索引数据存储

2009-03-30 11:27:59

中文域名

2021-07-02 06:54:43

分布式环境ID

2011-07-28 10:02:03

LightSwitch

2024-03-11 05:00:00

Python集合开发

2017-09-05 09:18:54

OracleCLOB大数据

2021-06-15 06:50:08

索引字段数据

2022-07-11 13:34:13

数据归档

2023-01-12 17:46:37

分库分表id如何生成

2015-07-22 17:21:34

Oracle数据字典

2019-10-21 09:55:12

数据库PostgreSQL Oracle

2010-12-06 09:10:02

LightSwitch

2023-10-26 08:28:31

Python数据去重

2016-01-25 09:38:24

云存储公共云

2023-10-30 10:40:29

检查用户app注册数据库

2018-03-16 15:30:45

数据库MySQL数据字典

2011-02-24 13:55:12

SQL Server可空字段非空值

2011-08-18 11:18:25

Oracle唯一约束唯一索引
点赞
收藏

51CTO技术栈公众号