如何在MVC 5中验证UNIQUE KEY [英] how to validate UNIQUE KEY in mvc 5

查看:149
本文介绍了如何在MVC 5中验证UNIQUE KEY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
请帮助我如何在模态中使用此代码验证唯一密钥

[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.

推荐答案

#^"& *)不允许使用名称.)]
[StringLength(3)]
[Column(TypeName = char)]
公共字符串Branch_Cde {get;放; }

如果我给相同的分支代码,那么它将发生异常.如何验证上述唯一密钥.
#^"&* ) 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.


您需要一个基础模型",从中继承唯一ID.

You need a "Base Model" from where you will inherit the unique ID.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆