C# 如何使用 ASP.net EF Codefirst 数据注释将 SQL Server 中的列设置为 varchar(max)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11235847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:04:47  来源:igfitidea点击:

How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations?

c#sql-serverentity-frameworkef-code-first

提问by Maddhacker24

I've been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max).

我一直在网上搜索,试图找出正确的语法,让 Entity Framework Code First 用一列创建我的表:varchar(max)。

This is what I have. By default this creates varchar(128). How do I create varchar(max)?

这就是我所拥有的。默认情况下,这会创建 varchar(128)。如何创建 varchar(max)?

I have tried [MaxLength] without success.

我试过 [MaxLength] 没有成功。

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

[Column(TypeName = "varchar")]
public string MediaDesc { get; set; }

采纳答案by Slauma

[Column(TypeName = "varchar(MAX)")]

Surprisingly the most obvious solution works.

令人惊讶的是,最明显的解决方案有效。

The [MaxLength]attribute only creates a varcharcolumn with a max length that isn't MAX but - in my case (SQL Server Express 2008 R2) - 8000.

[MaxLength]属性仅创建一个varchar最大长度不是 MAX 但 - 在我的情况下(SQL Server Express 2008 R2) - 8000 的列。

回答by bhuvin

Use [MaxLength] annotation.

使用 [MaxLength] 注释。

[Column(TypeName = "varchar")]
[MaxLength]
public string MediaDesc { get; set; }

回答by Diego Mijelshon

This will get you nvarchar(max):

这会让你nvarchar(max)

[StringLength(int.MaxValue)]

I don't think there's an attribute to force non-unicode (are you sure you want that?), so for varchar(max)you need a tweak in the DbContext:

我不认为有强制非 unicode 的属性(你确定你想要吗?),所以varchar(max)你需要在 DbContext 中进行调整:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Entity>().Property(x => x.MediaDesc).IsUnicode(false);
}

回答by cResults

[Column(TypeName = "varchar(MAX)")]results in varchar(Max) in the database. If you have a custom convention setting strings MaxLength to 100, modelBuilder .Properties<string>() .Configure(c => c.IsUnicode(false).HasMaxLength(100)); you need to include the MaxLength attribute [Column(TypeName = "varchar(MAX)"), MaxLength]. If you dont, you will get varchar(MAX) in the database but validation will throw for more that 100 characters

[Column(TypeName = "varchar(MAX)")]结果是数据库中的 varchar(Max)。如果您有自定义约定将字符串 MaxLength 设置为 100,则 modelBuilder .Properties<string>() .Configure(c => c.IsUnicode(false).HasMaxLength(100)); 需要包含 MaxLength 属性[Column(TypeName = "varchar(MAX)"), MaxLength]。如果不这样做,您将在数据库中获得 varchar(MAX) 但验证将抛出超过 100 个字符

回答by Ogglas

Update for @Slauma answer.

更新@Slauma 答案。

Using a override for all strings like this in OnModelCreating:

对所有这样的字符串使用覆盖OnModelCreating

modelBuilder.Properties<string>().Configure(s => 
    s.HasMaxLength(256).HasColumnType("nvarchar"));

and then modifying properties with attributes like this:

然后使用如下属性修改属性:

[Column(TypeName = "nvarchar(MAX)")]
public string CaseComment { get; set; }

Or this:

或这个:

modelBuilder.Entity<YourClass>()
    .Property(b => b.CaseComment)
    .HasColumnType("nvarchar(MAX)");

This can cause the Exception. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.Even though the column is of correct data type Entity Framework still thinks it is nvarchar(256)and throws the error DbEntityValidationException.

这可能会导致异常。Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.即使该列的数据类型正确,实体框架仍然认为它是nvarchar(256)并抛出错误DbEntityValidationException

To fix this use the following instead:

要解决此问题,请改用以下内容:

[Column(TypeName = "nvarchar(MAX)")]
[MaxLength]
public string CaseComment { get; set; }

Or

或者

modelBuilder.Entity<YourClass>()
    .Property(b => b.CaseComment)
    .HasColumnType("nvarchar(MAX)")
    .HasMaxLength(null);