Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架唯一键与fluent api?_C#_Entity Framework 6 - Fatal编程技术网

C# 实体框架唯一键与fluent api?

C# 实体框架唯一键与fluent api?,c#,entity-framework-6,C#,Entity Framework 6,是否有任何方法可以使用Fluent API指定索引应该是唯一的,而不将其作为数据注释添加到模型本身中 public class RecordType { public int Id { get; set; } [Index(IsUnique = true)] public string RecordType { get; set; } } 如何在下面的代码中添加唯一索引 public class RecordTypeConfiguration : EntityType

是否有任何方法可以使用Fluent API指定索引应该是唯一的,而不将其作为数据注释添加到模型本身中

public class RecordType
{
    public int Id { get; set; }

    [Index(IsUnique = true)]
    public string RecordType { get; set; }
}
如何在下面的代码中添加唯一索引

public class RecordTypeConfiguration : EntityTypeConfiguration<RecordType>
{
    public RecordTypeConfiguration()
    {
       HasKey(i => i.Id);
       Property(e => e.RecordType).HasColumnType("VARCHAR").HasMaxLength(1);   
    }
}
公共类RecordTypeConfiguration:EntityTypeConfiguration
{
公共记录类型配置()
{
HasKey(i=>i.Id);
属性(e=>e.RecordType).HasColumnType(“VARCHAR”).HasMaxLength(1);
}
}

我想你可以使用IndexAttribute:

   this.Property(t => t.RecordType)
   .HasColumnAnnotation(
   "Index",
   new IndexAnnotation(new IndexAttribute("_RecordType") { IsUnique = true }));

实体框架>=6.2,

在DbContext中:

模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasIndex(u=>u.RecordType).IsUnique();
}

实体框架<6.2

在DbContext中:

模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().Property(t=>t.RecordType).HasMaxLength(1)
.HasColumnAnnotation(IndexAnnotation.AnnotationName,新的IndexAnnotation(新的IndexAttribute(“Ix_RecordType”){IsUnique=true}));
}
或在单独的配置文件中:

公共类RecordTypeConfiguration:EntityTypeConfiguration
{
公共记录类型配置()
{
HasKey(i=>i.Id);
属性(e=>e.RecordType).HasColumnType(“VARCHAR”).HasMaxLength(1);
属性(t=>t.RecordType).HasColumnAnnotation(IndexAnnotation.AnnotationName,新的IndexAnnotation(新的IndexAttribute(“Ix_RecordType”){IsUnique=true}));
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Entity<RecordType>().HasIndex(u => u.RecordType).IsUnique();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Entity<RecordType>().Property(t => t.RecordType).HasMaxLength(1)
                                  .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("Ix_RecordType"){IsUnique = true}));
}
public class RecordTypeConfiguration : EntityTypeConfiguration<RecordType>
{
    public RecordTypeConfiguration()
    {
        HasKey(i => i.Id);

        Property(e => e.RecordType).HasColumnType("VARCHAR").HasMaxLength(1);

        Property(t => t.RecordType).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("Ix_RecordType"){IsUnique = true}));

     }
}