我如何指定一个属性应该生成一个文本列,而不是一个nvarchar(4000) [英] How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

查看:122
本文介绍了我如何指定一个属性应该生成一个文本列,而不是一个nvarchar(4000)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与实体框架的code首先特点的工作,我试图找出当数据库是自动生成的,我可以如何指定要创建的列数据类型。

I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated.

我有一个简单的模型:

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数据库会自动与下面的模式创建:

When I run my application, a SQL CE 4.0 database is automatically created with the following schema:

到目前为止,一切都很好!不过,我会插入到正文数据 BodyHtml 特性是定期比对的最大允许长度的的NVarChar 列类型,所以我想EF生成这些属性的文字列。

So far, so good! However, the data I will be inserting into the Body and BodyHtml properties is routinely larger than the maximum allowed length for the NVarChar column type, so I want EF to generate Text columns for these properties.

不过,我似乎无法找到一个方法来做到这一点!相当多的谷歌搜索和阅读后,我尝试使用指定列类型 DataAnnotations ,从<一个找到的信息href=\"http://stackoverflow.com/questions/4829776/generate-money-type-fields-using-$c$c-first-ef-ctp5/4833477#4833477\">this回答:

However, I cannot seem to find a way to do this! After quite a bit of Googling and reading, I tried to specify the column type using DataAnnotations, from information found in this answer:

using System.ComponentModel.DataAnnotations;

...

[Column(TypeName = "Text")]
public string Body { get; set; }

这引发以下异常(当数据库被删除,应用程序是重办):

This throws the following exception (when the database is deleted and the app is re-run):

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.

但我不知道我应该指定什么名称空间或别名,我找不到任何会告诉我的。

But I have no idea what namespace or alias I should be specifying, and I couldn't find anything that would tell me.

我也试图改变注释按<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.dbtype.aspx\">this参考:

using System.Data.Linq.Mapping;

...

[Column(DbType = "Text")]
public string Body { get; set; }

在这种情况下,数据库中的的创建,但机身列仍然是的NVarChar(4000),所以它似乎注释被忽略。

In this case a database is created, but the Body column is still an NVarChar(4000), so it seems that annotation is ignored.

谁能帮助?这似乎是它应该是一个相当普遍的要求,但我的搜索已经无果而终!

Can anyone help? This seems like it should be a fairly common requirement, yet my searching has been fruitless!

推荐答案

我AP preciate是走进了现有的答案的努力,但我还没有发现它实际上回答的问题......所以我测试,并且发现了

I appreciate the effort that went into the existing answer, but I haven't found it actually answering the question... so I tested this, and found out that

[Column(TypeName = "ntext")]
public string Body { get; set; }

(从一个 System.ComponentModel.DataAnnotations )将努力创造一个NTEXT类型列。

(the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column.

(我与接受答案的问题是,它似乎表明,你应该改变在界面中列的类型,但问题是如何做到这一点编程。)

(My problem with the accepted answer is that it seems to indicate that you should change the column type in the interface, but the question is how to do it programmatically.)

这篇关于我如何指定一个属性应该生成一个文本列,而不是一个nvarchar(4000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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