LightSwitch 2011 数据字段唯一性验证方案
LightSwitch 2011 数据字段唯一性验证方案
验证单表数据的某个字段不能输入重复值
设置实体字段唯一索引
如果不写代码,那么验证只会在用户提交[保存]数据后,会提示错误,很明显这样的用户体验并不好,因此还需要做以下步骤
添加自定义验证
View Code
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));
}
{
// 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));
}
运行结果如下
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步