gpt4 book ai didi

c# - 如何指定属性应生成 TEXT 列而不是 nvarchar(4000)

转载 作者:IT王子 更新时间:2023-10-29 03:50:15 25 4
gpt4 key购买 nike

我正在使用 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

到目前为止,还不错!但是,我将插入到 BodyBodyHtml 属性中的数据通常大于 NVarChar 列类型允许的最大长度,因此我希望 EF 为这些属性生成 Text 列。

但是,我似乎找不到办法做到这一点!经过大量谷歌搜索和阅读后,我尝试使用 DataAnnotations 指定列类型,根据 this answer 中的信息:

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.

但我不知道应该指定什么命名空间或别名,而且我找不到任何可以告诉我的信息。

我还尝试根据 this reference 更改注释:

using System.Data.Linq.Mapping;

...

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

在这种情况下,创建了一个数据库,但是Body 列仍然是一个NVarChar(4000),所以看起来注释是忽略。

有人可以帮忙吗?这看起来应该是一个相当普遍的需求,但我的搜索没有结果!

最佳答案

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

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

(来自 System.ComponentModel.DataAnnotations 的那个)将用于创建一个 ntext 类型的列。

(我对接受的答案的问题是,它似乎表明您应该更改界面中的列类型,但问题是如何以编程方式进行。)

关于c# - 如何指定属性应生成 TEXT 列而不是 nvarchar(4000),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4895465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com