gpt4 book ai didi

sql-server - Entity Framework 7 迁移 : how to get a varchar column with a length greater than 1?

转载 作者:行者123 更新时间:2023-12-03 18:19:25 25 4
gpt4 key购买 nike

我有以下内容:

[Table("Countries")]
public class Country
{
public int CountryId { get; set; }

[MaxLength(255), Required]
public string CountryName { get; set; }

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

每次我应用迁移时,CountryCode 都会变成最大长度为 1 的 varchar 列。我尝试将 MaxLength 注释设置为 255,但最大长度仍然为 1。当列设置为 nvarchar 时,它会按预期工作.我做错了什么吗?

编辑:每当我显式设置字符串数据类型时,长度设置为 1。我可以使用 Column(TypeName = "nvarchar")] 并将长度设置为 1..

最佳答案

很想使用数据注释,但我能够使用它:

 builder.Entity<Country>()
.Property(e = e.CountryCode).HasSqlServerColumnType("VARCHAR(30)");

Data Annotations are not yet fully implemented in Entity Framework 7. You can still add annotations to your entity classes so that they are used by other frameworks (such as ASP.NET MVC), but Entity Framework will not process these annotations. You can track support for Data Annotations on GitHub.

http://ef.readthedocs.org/en/latest/modeling/configuring.html

关于sql-server - Entity Framework 7 迁移 : how to get a varchar column with a length greater than 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252188/

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