Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add modifications to your model when using EF Core DB First Design

My team uses Db first design. We create the database, then create the model using the Scaffold-DbContext command.

The problem is when we need to modify the model and then do a recreation.

public partial class UserInfo
{
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public DateTime RecordCreated { get; set; }
}

Upon invoking a Scaffold-DbContext with the -Force it will remove the [Required] from it.

Should I be looking at using a ViewModel, creating partial classes or what?

Very early on in using EF core 2.1 so any help would be greatly appreciated.

Joe

like image 857
Joe Ruder Avatar asked Dec 06 '25 18:12

Joe Ruder


1 Answers

Use EF Core Power Tools, it generates a partial method for OnModelCreating, and then use the fluent API in a new partial class to set the Required option instead of attributes.

like image 70
ErikEJ Avatar answered Dec 10 '25 21:12

ErikEJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!