未生成EF 6.1索引IsUnique [英] EF 6.1 Index IsUnique not being generated

查看:246
本文介绍了未生成EF 6.1索引IsUnique的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先拥有以下代码:

class Role
{
    [Key]
    public int Id { get; set; }
    [Index("NameIndex", IsUnique = true)]
    public string Name { get; set; }
}

但是在使用SQL Management Studio检查数据库时,会有多个同名行:

But on checking the database with SQL Management Studio, there are multiple rows of the same name:

ID=1, Name=admin
ID=2, Name=admin

我的情况很简单:

class MemberContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
}

检查SQL 2014 Express数据库表索引,不会生成任何索引(仅存在PK).

Checking the SQL 2014 Express database table indexes, none are generated (only the PK exists).

有没有我需要做的工作才能使Index正常工作?

Is there anything I have not done that is required for Index to work?

推荐答案

因此,我正在编写一个新类(由Autofac从DLL注入),并且主程序正在运行EF 6.0(而我的实用程序类项目是使用EF 6.1).

So I was writing a new class (that was being injected from a DLL by Autofac) and the main program was running EF 6.0 (whereas my utility class project was using EF 6.1).

具体来说,我必须在字符串长度上设置长度限制,否则我将无法在其上放置索引(无法在varchar(max)上生成索引):

class Role
{
    [Key]
    public int Id { get; set; }

    [MaxLength(127)]
    [Index(IsUnique = true)]
    public string Name { get; set; }       

    public virtual ICollection<User> Users { get; set; }
}

这篇关于未生成EF 6.1索引IsUnique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆