c# - 如何在实体中的使用表中创建 PK 和唯一用户名(使用代码优先)?

标签 c# sql-server asp.net-mvc entity-framework-6 entity-framework-migrations

我想设置如下。
我正在使用代码优先方法的最新 Entity Framework ..

Public class AppUser
{
        [Key]
        public int Id { get; set; }
        [Required]
        [Index(IsUnique = true)]
        public string UserName { get; set; }
}

但是它不起作用..

示例记录如下

enter image description here

收到以下错误信息:

"exceptionmessage": "Column 'UserName' in table 'dbo.ApplicationUser' is of a type that is invalid for use as a key column in an index.",

最佳答案

您需要限制UserName 属性的长度。为此使用 StringLength 注释。

If the size of all fixed key columns plus the minimum size of all variable columns specified in the CREATE INDEX statement exceeds 900 bytes, the CREATE INDEX statement fails.

来源:http://msdn.microsoft.com/en-us/library/ms191241.aspx

关于c# - 如何在实体中的使用表中创建 PK 和唯一用户名(使用代码优先)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45386800/

相关文章:

c# - 使用正则表达式获取多个 HTML 标签之间的文本

sql - 在 R 中批量插入

c# - Entity Framework 为每种类型的记录选择最近的记录

asp.net-mvc - 在 MVC Controller 中访问 GET 参数

sql - Entity Framework 在插入其他表时未引用的表中创建新记录

c# - 使用ftp c#高效异步上传多个文件

c# - 获取类类型?

c# - 后台 worker C# winform

sql-server - 为什么在 SQL Server 中的索引列上执行 top(1) 速度很慢?

sql-server - 执行计划上的 CTE 性能。是显示两次,还是处理了两次?