c# - 如何首先在 EF6 代码中设置一个唯一的属性

标签 c# entity-framework entity-framework-6

<分区>

我有这门课:

public class BSC
{
    public int BSCId { get; set; }

    public string BSCName { get; set; }
}

和配置类:

public class BSCConfig :EntityTypeConfiguration<BSC>
{
    public BSCConfig()
    {
        Property(m => m.BSCName).HasMaxLength(50).HasColumnName("Category").IsRequired();

    }
}

我想使这个属性成为唯一的,但我没有 isUnique 或 Index 方法。

你能告诉我如何使这个属性独一无二吗?

最佳答案

使用 HasColumnAnnotation :

Property(m => m.BSCName).HasMaxLength(50).HasColumnName("Category").IsRequired()
  .HasColumnAnnotation("Index",
   new IndexAnnotation(new IndexAttribute("IX_X_Category") { IsUnique = true }));

关于c# - 如何首先在 EF6 代码中设置一个唯一的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295777/

相关文章:

c# - 通过 C# 代码解析/重构 C#

c# threading with manual reset 事件

c# - Entity Framework 可选 :optional relationship

c# - EF 6.0 中缺少 DbSet<entity>.Load() 函数

c# - Code First 和 DBContext 的多态性

c# - 如何在 PocoDynamo 中创建带空字段的 QueryExpression

c# - 如何声明返回相同类型的 Func Delegate 的 Func Delegate?

entity-framework - Entity Framework : Conditional foreign key

c# - 如何使用linq C#展平列表

c# - 我应该将 Entity Framework 包含在我的类库中还是直接包含在应用程序中?