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

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

问题描述

我正在使用Entity Framework的Code First功能,我试图找出如何指定在数据库自动生成时应该创建的列数据类型。

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:

到目前为止,太好了!但是,我将插入到 Body BodyHtml 属性中的数据通常大于 NVarChar 列类型,所以我希望EF为这些属性生成 Text 列。

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.

但是,我似乎找不到一个办法来做到这一点!经过相当多的Google搜索和阅读后,我尝试使用 DataAnnotations 这个答案

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.

我也尝试根据此参考

using System.Data.Linq.Mapping;

...

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

在这种情况下,创建了数据库 ,但 Body 列仍然是一个 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!

推荐答案

我感谢所做的努力现在的答案,但我没有发现它实际上回答了这个问题...所以我测试了这一点,并发现

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.)

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

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