ایجاد ایندکس منحصربفرد بر روی چند فیلد با هم در EF Code first
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۵/۱۸ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public abstract class BaseEntity
{
public int Id { get; set; }
}
public class User : BaseEntity
{
[Index(IsUnique = true)]
public string EmailAddress { get; set; }
} public class UserRating : BaseEntity
{
public VoteSectionType SectionType { set; get; }
public double RatingValue { get; set; }
public int SectionId { get; set; }
[ForeignKey("UserId")]
public virtual User User { set; get; }
public int UserId { set; get; }
} public class UserRating : BaseEntity
{
[Index("IX_Single_UserRating", IsUnique = true, Order = 1)] //کلید منحصربفرد ترکیبی روی سه ستون
public VoteSectionType SectionType { set; get; }
public double RatingValue { get; set; }
[Index("IX_Single_UserRating", IsUnique = true, Order = 2)]
public int SectionId { get; set; }
[ForeignKey("UserId")]
public virtual User User { set; get; }
[Index("IX_Single_UserRating", IsUnique = true, Order = 3)]
public int? UserId { set; get; }
} CREATE UNIQUE INDEX [IX_Single_UserRating] ON [UserRatings] ([SectionType] ASC,[SectionId] ASC,[UserId] ASC);
modelBuilder.Entity<People>()
.Property(x => x.Firstname)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ix_people_firstname"))); modelBuilder.Entity<People>()
.Property(x => x.Firstname)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ix_people_fullname", 1)));
modelBuilder.Entity<People>()
.Property(x => x.Lastname)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ix_people_fullname", 2))); modelBuilder.Entity<People>()
.Property(x => x.NationalInsuranceNo)
.HasColumnAnnotation("Index",
new IndexAnnotation(new IndexAttribute("ix_people_nationalinsurance") {IsUnique = true})); modelBuilder.Entity<People>()
.Property(x => x.Id)
.HasColumnAnnotation("Index",
new IndexAnnotation(new IndexAttribute("ix_people_nationalinsurance") { IsClustered = true}));