Home > Software engineering >  How to add an annotation to property after updating database using migrations
How to add an annotation to property after updating database using migrations

Time:11-25

I'm creating an MVC ASP.NET Core 3.1 Web application, and in the middle of the project I added the following properties and annotations to the ApplicationUser class.

    [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; }

However, I accidentally run add-migration and update-database before adding another annotation to DateOfBirth:

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

What is the way to correct the mistake? How can I add the annotation now? Thank you.

CodePudding user response:

You can add another annotation to DateOfBirth now. Because changing the Display data annotation for a property won’t have influence on the database schema.

There is a similar problem here:

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

I find the related document that code first data annotations:

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

  • Related