用Code创建字符串索引 [英] Creating string index with Code first

查看:140
本文介绍了用Code创建字符串索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类项目
{
[Index]
public string CreatedBy {set;得到;
}

当我使用update-database进行迁移时,我收到以下错误。然而,据我研究的 [Index] 应该作为注释到 string


表'dbo.Items'中的列'CreatedBy'是一种无法用作索引中的关键列的类型。



解决方案

通常,当您使用VARCHAR(Max)尝试使用:


$时,您会收到此错误b $ b

  [Column(TypeName =VARCHAR)] 
[StringLength(n)]
[索引]
public string CreatedBy {组;得到; }

其中n在1到450之间。


I'm using Entity Framework 6.1 code-first and my domain model is below.

class Item
{
    [Index]
    public string CreatedBy { set; get; }
} 

When I use update-database for migration, I get the following error. However as far as I researched [Index] should work as annotation to string.

Column 'CreatedBy' in table 'dbo.Items' is of a type that is invalid for use as a key column in an index.

解决方案

Usually you get this error when you use a VARCHAR(Max) try using:

[Column(TypeName = "VARCHAR")]
[StringLength(n)]
[Index]
public string CreatedBy { set; get; }

where n is between 1 and 450.

这篇关于用Code创建字符串索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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