Answers:
在EF6.2上,您可以HasIndex()
用来添加索引以通过fluent API进行迁移。
https://github.com/aspnet/EntityFramework6/issues/274
例
modelBuilder
.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
从EF6.1开始,您可以使用IndexAnnotation()
fluent API添加索引以进行迁移。
http://msdn.microsoft.com/zh-cn/data/jj591617.aspx#PropertyIndex
您必须添加对以下内容的引用:
using System.Data.Entity.Infrastructure.Annotations;
基本范例
这是一个简单的用法,在User.FirstName
属性上添加索引
modelBuilder
.Entity<User>()
.Property(t => t.FirstName)
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
实际示例:
这是一个更现实的例子。它将在多个属性上添加唯一索引:User.FirstName
和User.LastName
,索引名称为“ IX_FirstNameLastName”
modelBuilder
.Entity<User>()
.Property(t => t.FirstName)
.IsRequired()
.HasMaxLength(60)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(
new IndexAttribute("IX_FirstNameLastName", 1) { IsUnique = true }));
modelBuilder
.Entity<User>()
.Property(t => t.LastName)
.IsRequired()
.HasMaxLength(60)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(
new IndexAttribute("IX_FirstNameLastName", 2) { IsUnique = true }));
IndexAnnotation.AnnotationName
new IndexAttribute() { IsUnique = true }
。否则,它将创建常规(非唯一)索引。
除了Yorro的答案外,还可以使用属性来完成。
int
类型唯一键组合的示例:
[Index("IX_UniqueKeyInt", IsUnique = true, Order = 1)]
public int UniqueKeyIntPart1 { get; set; }
[Index("IX_UniqueKeyInt", IsUnique = true, Order = 2)]
public int UniqueKeyIntPart2 { get; set; }
如果数据类型为string
,则MaxLength
必须添加属性:
[Index("IX_UniqueKeyString", IsUnique = true, Order = 1)]
[MaxLength(50)]
public string UniqueKeyStringPart1 { get; set; }
[Index("IX_UniqueKeyString", IsUnique = true, Order = 2)]
[MaxLength(50)]
public string UniqueKeyStringPart2 { get; set; }
如果存在域/存储模型分离问题,Metadatatype
则可以选择使用属性/类:https : //msdn.microsoft.com/zh-cn/library/ff664465%28v=pandp.50%29.aspx?f= 255&MSPPError = -2147217396
快速控制台应用程序示例:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace EFIndexTest
{
class Program
{
static void Main(string[] args)
{
using (var context = new AppDbContext())
{
var newUser = new User { UniqueKeyIntPart1 = 1, UniqueKeyIntPart2 = 1, UniqueKeyStringPart1 = "A", UniqueKeyStringPart2 = "A" };
context.UserSet.Add(newUser);
context.SaveChanges();
}
}
}
[MetadataType(typeof(UserMetadata))]
public class User
{
public int Id { get; set; }
public int UniqueKeyIntPart1 { get; set; }
public int UniqueKeyIntPart2 { get; set; }
public string UniqueKeyStringPart1 { get; set; }
public string UniqueKeyStringPart2 { get; set; }
}
public class UserMetadata
{
[Index("IX_UniqueKeyInt", IsUnique = true, Order = 1)]
public int UniqueKeyIntPart1 { get; set; }
[Index("IX_UniqueKeyInt", IsUnique = true, Order = 2)]
public int UniqueKeyIntPart2 { get; set; }
[Index("IX_UniqueKeyString", IsUnique = true, Order = 1)]
[MaxLength(50)]
public string UniqueKeyStringPart1 { get; set; }
[Index("IX_UniqueKeyString", IsUnique = true, Order = 2)]
[MaxLength(50)]
public string UniqueKeyStringPart2 { get; set; }
}
public class AppDbContext : DbContext
{
public virtual DbSet<User> UserSet { get; set; }
}
}
这是用于更流畅地设置唯一索引的扩展方法:
public static class MappingExtensions
{
public static PrimitivePropertyConfiguration IsUnique(this PrimitivePropertyConfiguration configuration)
{
return configuration.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute { IsUnique = true }));
}
}
用法:
modelBuilder
.Entity<Person>()
.Property(t => t.Name)
.IsUnique();
将产生迁移,例如:
public partial class Add_unique_index : DbMigration
{
public override void Up()
{
CreateIndex("dbo.Person", "Name", unique: true);
}
public override void Down()
{
DropIndex("dbo.Person", new[] { "Name" });
}
}
不幸的是,实体框架不支持此功能。它在EF 6的路线图上,但被推迟了:Workitem 299:唯一约束(唯一索引)
modelBuilder.Property(x => x.FirstName).IsUnicode().IsRequired().HasMaxLength(50);