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

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

How do I specify the maximum column length of a field using entity framework code first

c#.netentity-frameworkentity-framework-5system.componentmodel

提问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)

  • Configuring Properties and Types with the Fluent API

or if you want Data Annotation, use MaxLengthand MinLengthattributes

或者如果你想Data Annotation,使用MaxLengthMinLength属性

public class EntityRegister
{
    public int Id { get; set; }
    [MaxLength(450)]
    public string Name { get; set; }
}