C# 如何首先使用实体框架代码指定字段的最大列长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14008443/
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
How do I specify the maximum column length of a field using entity framework code first
提问by Kirsten Greed
Is there an attribute I can use when creating a table ? I tried [StringLength]
but it seems to be ignored.
创建表时是否有可以使用的属性?我试过了,[StringLength]
但似乎被忽略了。
public class EntityRegister
{
public int Id { get; set; }
[StringLength(450)]
public string Name { get; set; }
}
采纳答案by John Woo
alternatively, you can manually do it on Fluent API
或者,您可以手动执行 Fluent API
use HasMaxLength(450)
用 HasMaxLength(450)
or if you want Data Annotation
, use MaxLength
and MinLength
attributes
或者如果你想Data Annotation
,使用MaxLength
和MinLength
属性
public class EntityRegister
{
public int Id { get; set; }
[MaxLength(450)]
public string Name { get; set; }
}