我正在使用 Entity Framework 的 Code First 功能,并试图弄清楚如何指定在自动生成数据库时应该创建的列数据类型。
我有一个简单的模型:
public class Article
{
public int ArticleID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Summary { get; set; }
public string SummaryHtml { get; set; }
public string Body { get; set; }
public string BodyHtml { get; set; }
public string Slug { get; set; }
public DateTime Published { get; set; }
public DateTime Updated { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
当我运行我的应用程序时,会使用以下架构自动创建一个 SQL CE 4.0 数据库:
到现在为止还挺好!但是,我将插入到Body
andBodyHtml
属性中的数据通常大于NVarChar
列类型的最大允许长度,因此我希望 EF 为这些属性生成Text
列。
但是,我似乎无法找到一种方法来做到这一点!经过相当多的谷歌搜索和阅读后,我尝试使用DataAnnotations
从这个答案中找到的信息指定列类型:
using System.ComponentModel.DataAnnotations;
...
[Column(TypeName = "Text")]
public string Body { get; set; }
这会引发以下异常(当数据库被删除并重新运行应用程序时):
Schema specified is not valid. Errors: (12,6) : error 0040: The Type text is not qualified with a namespace or alias. Only PrimitiveTypes can be used without qualification.
但我不知道我应该指定什么命名空间或别名,也找不到任何可以告诉我的信息。
我还尝试根据此参考更改注释:
using System.Data.Linq.Mapping;
...
[Column(DbType = "Text")]
public string Body { get; set; }
在这种情况下,创建了一个数据库,但该Body
列仍然是NVarChar(4000)
,因此似乎忽略了注释。
任何人都可以帮忙吗?这似乎应该是一个相当普遍的要求,但我的搜索一直没有结果!