首先使用代码创建字符串索引 [英] Creating string index with Code first

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

问题描述

我使用 Entity Framework 6.1 代码优先,我的域模型如下.

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

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

当我使用 update-database 进行迁移时,出现以下错误.但是,据我研究 [Index] 应该可以作为 string 的注释.

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.

表 'dbo.Items' 中的列 'CreatedBy' 的类型不能用作索引中的键列.

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

推荐答案

通常您在使用 VARCHAR(Max) 时会遇到此错误尝试使用:

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

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

其中 n 介于 1 和 450 之间.

where n is between 1 and 450.

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

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