问题描述
大家好,
请帮助我如何在模态中使用此代码验证唯一密钥
[Index(IsUnique = true)]
[Required(ErrorMessage =必须输入分支代码.")]
[RegularExpression(@ ^ [a-zA-Z0-9''''''] +,ErrorMessage =特殊字符(,@/)(=] [| \!`'%$#^"& *)是名称中不允许.)]
[StringLength(3)]
[Column(TypeName = char)]
公共字符串Branch_Cde {get;放; }
如果我给相同的分支代码,那么它将发生异常.如何验证以上唯一密钥.
hi to all,
Kindly help me how to validate unique key here i''m using this code in modal
[Index(IsUnique = true)]
[Required(ErrorMessage = "Branch Code is required.")]
[RegularExpression(@^[a-zA-Z0-9'''' '']+, ErrorMessage =Special characters ( ,@/)(=][|\!`’%$#^"&* ) are not allowed in the name.)]
[StringLength(3)]
[Column(TypeName = char)]
public string Branch_Cde { get; set; }
if i give same Branch code then it will occur exception. how to validate above unique key.
推荐答案
public class BaseModel<t> where T : BaseModel<t>
{
[Key, Required, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
// You can do a lot of very useful code here...
}
public class PersonModel : BaseModel<personmodel>
{
public string Name { get; set; }
public DateTime Birth{ get; set; }
}
并将其添加到您的entityframework上下文中
And of course add it to your entityframework context
public class SMSServiceContext : DbContext
{
public SMSServiceContext()
: base()
{
}
public DbSet<personmodel> PersonModels{ get; set; }
// Add other tables here...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
唯一密钥将是自动且唯一的.无论如何,这就是我处理实体框架时的方法.
希望我回答了你的问题.如果没有,请通知我:)
The unique Key will be automatic and unique. This is how I do it when I work with entityframework anyways.
I hope I answered your question. If not, please inform me :)
这篇关于如何在MVC 5中验证UNIQUE KEY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!