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

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

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

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

  设置实体字段唯一索引

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

  添加自定义验证

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

2024-05-24 09:29:28

2021-05-20 08:16:57

数据库数据软删除数据

2022-01-27 11:02:04

索引数据存储

2011-07-28 10:02:03

LightSwitch

2009-03-30 11:27:59

中文域名

2021-07-02 06:54:43

分布式环境ID

2024-11-28 09:47:53

C#互斥锁Mutex

2024-03-11 05:00:00

Python集合开发

2017-09-05 09:18:54

OracleCLOB大数据

2021-06-15 06:50:08

索引字段数据

2024-09-05 16:55:41

2024-08-29 09:27:44

LuceneES字段

2022-07-11 13:34:13

数据归档

2023-01-12 17:46:37

分库分表id如何生成

2015-07-22 17:21:34

Oracle数据字典

2010-12-06 09:10:02

LightSwitch

2019-10-21 09:55:12

数据库PostgreSQL Oracle

2023-10-26 08:28:31

Python数据去重

2016-01-25 09:38:24

云存储公共云
点赞
收藏

51CTO技术栈公众号