sqliteasp.net-coreblazor.net-9.0

How to properly modify the data model and update sqlite db in .NET 9 in Blazor web app?


My issue is somewhat related to these question threads (but not applicable due to being outdated and with a different overall context):

In VS Code using C# and a Blazor web app (on .NET 9.0), more specifically, my error messages are as follows.

My thought process is by right-clicking the {MyBlazorWebAppName} project file ({MyBlazorWebAppName}.csproj) and selecting Open in Integrated Terminal, as I believe I need to update the database with my changes to the data model.

However, after executing the command:

{folder directory}\MyBlazorWebAppName\MyBlazorWebAppName> dotnet ef database update

I got this build failed message:

Build started...
Build failed. Use dotnet build to see the errors.

Moreover, by executing dotnet build or Ctrl-Shift+P .NET: Build, I got this error message:

error CS1061: 'Admin' does not contain a definition for 'FullName' and no accessible extension method 'FullName' accepting a first argument of type 'Admin' could be found (are you missing a using directive or an assembly reference?)

What have I done before encountering those errors:

In my Models/Admin.cs - old data model:

using System.ComponentModel.DataAnnotations;

namespace MyBlazorWebAppName.Models;

public class Admin
{
    [Key]
    public string? UserID { get; set; }
    public string? Username { get; set; }
    public string? FullName { get; set; }
    public string? EmailAdd { get; set; }
    public string? RoleType { get; set; }
}

With changes applied:

using System.ComponentModel.DataAnnotations;

namespace MyBlazorWebAppName.Models;

public class Admin
{
    [Key]
    public string? UserID { get; set; }
    public string? Username { get; set; }
    public string? GivenName { get; set; }
    public string? MiddleName { get; set; }
    public string? FamilyName { get; set; }
    public string? EmailAdd { get; set; }
    public string? RoleType { get; set; }
}

As you may have noticed, I replaced FullName with GivenName, MiddleName, and FamilyName.

I'm uncertain how to apply my data model changes to my SQLite database.

Note: I've followed and completed the tutorial for this particular section: Build a Blazor movie database app (Part 2 - Add and scaffold a model)


Solution

  • You need to create a new migration before updating the database with the new schema. A migration tells EF Core how to switch from one schema to another.

    You actually already created a migration containing your initial data model during the tutorial, now you just need to do it again.

    dotnet ef migrations add NewNameSchema