【问题标题】:How to add an annotation to property after updating database using migrations使用迁移更新数据库后如何向属性添加注释
【发布时间】:2022-01-01 11:19:03
【问题描述】:

我正在创建一个 MVC ASP.NET Core 3.1 Web 应用程序,并在项目的中间向 ApplicationUser 类添加了以下属性和注释。

    [Display(Name = "First Name")]
    [StringLength(100, ErrorMessage = "The {0} can't have more than 100 characters.")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [StringLength(100, ErrorMessage = "The {0} can't have more than 100 characters.")]
    public string LastName { get; set; }

    [Display(Name = "Date Of Birth")]
    public DateTime DateOfBirth { get; set; }

    public string Country { get; set; }

但是,在向 DateOfBirth 添加另一个注释之前,我不小心运行了 add-migration 和 update-database:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]

改正错误的方法是什么?我现在如何添加注释? 谢谢。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core data-annotations asp.net-core-3.1


    【解决方案1】:

    您现在可以向 DateOfBirth 添加另一个注释。因为更改属性的显示数据注释不会影响数据库架构。

    这里也有类似的问题:

    Do we still need to run Add-Migration if we change only Display attribute of data Annotation

    我找到了编码第一个数据注释的相关文档:

    https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      相关资源
      最近更新 更多