c# - 如何字符串字段是唯一的 mvc 5 asp.net

标签 c# asp.net-mvc unique

<分区>

public class Sport
{

    public int SportID { get; set; }

    [Required(ErrorMessage = "The Name field is required.")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Start Date")]
    public DateTime StartDate { get; set; }

    [Column(TypeName = "money")]
    public decimal Price { get; set; }

    public int? InstructorID { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }

    public virtual ICollection<Instructor> Instructors { get; set; }
    public virtual ICollection<Group> Groups { get; set; }

}

此表中的关键字是 SportID。 我可以创建更多同名运动,但我不喜欢这个

如何让我的运动名称唯一?请帮我。谢谢

最佳答案

您正在寻找Index 数据注释。文档 here

[Required(ErrorMessage = "The Name field is required.")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
[StringLength(50, MinimumLength = 3)]
[Index]
public string Name { get; set; }

关于c# - 如何字符串字段是唯一的 mvc 5 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29274863/

相关文章:

c# - 为什么使用MessageQueue存储异常消息?

asp.net-mvc - MVC3 服务层验证。返回异常、自定义对象、模型状态字典?

unique - 允许每台计算机一票(非IP,未登录用户)

mysql - 使用 SQL RAND() 生成受约束的 DISTINCT 元素

c# - System.Serializable 在 Unity 中的 List<MyClass> 上不起作用?

c# - 如何在LINQ中执行2次检查

c# - .NET 中的 JSON 反序列化

asp.net - SqlCeException 数据库以只读连接打开

c# - 无法加载文件或程序集 'EntityFramework,版本 = 6.0.0.0,

c# - 找到基于两个输入的唯一输出?