3

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

Joe Ruder
  • 2,122
  • 2
  • 23
  • 52

4 Answers4

2

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.

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • Or just implement your OnModelCreating in a partial class file, and after re-scaffolding the database, manually delete the OnModelCreating method generated by the scaffolding. – David Browne - Microsoft Dec 01 '18 at 22:38
  • Obviously I need to do more reading, I am almost but not quite following both of you. I get the EF Core Power Tools, but then you lost me at how to use the fluent API. And I don't understand how to do what David is saying. – Joe Ruder Dec 01 '18 at 23:33
2

If you are using database first, you make the database column required (NOT NULL), and then run scaffolding again, not the other way round. When scaffolding, you can choose to generated Attributes over fluent configuration, if you do that, you will get the "Required" attribute added (for reference types).

The switch for Scaffold-Dbontext is -DataAnnotations

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • thank you...that is super interesting, I never saw the -DataAnnotations flag before. I will read that as soon as I get in today in a couple hours. I appreciate the update. – Joe Ruder Dec 02 '18 at 08:57
0

I understand that it was created DB first but after initial creation of the models if you do model changes before changes in db it would be best to create migrations from code that way all the changes to the models will be replicated in the database.

markorial
  • 425
  • 2
  • 12
  • There will be time (often) where another programmer will be changing the database. I may have no option except to use the Scaffold-DbContext -Force. I am needing to know how to do that. Several places confirms that this is the right work flow vs switching to migrations. I was told to use partial classes and am currently trying to get that to work. – Joe Ruder Dec 01 '18 at 22:05
  • Then yes partial classes are the way to go or model inheritance where the base model would be in one place and inherited model which is the same but with attributes and all is in another folder so the scaffold does not squash it. Also you can use mapping from persistent repo of models to models for views. Here is a nice link to get you started https://www.c-sharpcorner.com/article/mapping-similar-objects-in-asp-net-core-2-0/ – markorial Dec 02 '18 at 17:51
0

As pointed out by ErikEJ this won't work:

You could use a metadata class along with a partial class (example copied from doc):

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

[MetadataType(typeof(UserInfoMetaData))]
public partial class UserInfo
{

}

public class UserInfoMetaData
{
    [Required()]
    public object FirstName;
}

This would then sit in a separate file, that won't be touched by code-gen. Note that you don't need to add all properties to the metadata class and their type doesn't matter - but the names must match.

There are some ways however how to make it work, see this SO item which itself is based on this ASP.NET Forums question. In the ASP.net link, there is also a suggestion to make the validation on the view model instead of the data classes. So that might be a possibility to consider.

Marty
  • 412
  • 3
  • 7
  • @ErikEJ unfortunately I haven't worked with EF Core yet; but I expanded the answer. Thanks – Marty Dec 02 '18 at 11:03