我正在使用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数据库:
![DB Schema](https://istack.dev59.com/A4OI9.gif)
Body
和BodyHtml
属性中的数据通常大于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)
,因此似乎注释被忽略了。有人能帮忙吗?这似乎应该是一个相当常见的需求,但我的搜索一直没有结果!