开发者

Entity Framework Data Annotations Set StringLength VarChar

I have this setting in my model:

[Stri开发者_C百科ngLength(250)]
public string Comment { get; set; }

to set the maximum length to 250 in the database which is great.

However it's set as nvarchar(250) when the database person was expecting varchar(250).

Can somebody please tell me how to set it as a varchar from the model as opposed to an nvarchar?


Use ColumnAttribute to give the datatype

[Column(TypeName = "VARCHAR")]
[StringLength(250)]
public string Comment { get; set; }

Or use fluent API

modelBuilder.Entity<MyEntity>()
  .Property(e => e.Comment).HasColumnType("VARCHAR").HasMaxLength(250);


For some reason this older post keeps coming up in my search... so just FYI, using EF6 Core it's combined. The above answer errors out for me.

[Column(TypeName = "VARCHAR(250)")]
public string Comment {get;set;}


Visual Studio 2022 using Net 6 and EF Core 6, database first using the -DataAnnotations parameter creates the following attributes

 /* Nullable column */
 [StringLength(250)]
 [Unicode(false)]
 public string? Comment { get; set; }

 /* Non-Nullable column */
 [StringLength(250)]
 [Unicode(false)]
 public string Comment { get; set; } = null!;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜