EntityFramework - 唯一索引
参考链接:
http://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx
自定义约束
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class UniqueAttribute : ValidationAttribute { public override Boolean IsValid(Object value) { //校验数据库是否存在当前Key return true; } }
F
this.Property(p => p.Name) .IsRequired() .HasMaxLength(new int?(50)) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UX_Role_Name") { IsUnique=true}));
Dann
using System.ComponentModel.DataAnnotations.Schema; [Index(IsUnique = true)]