Skip to content

Create Migrations & Adding Tables

Keith edited this page Oct 20, 2021 · 13 revisions

This is an idiots guide to adding Entity Framework Models to your project.

  1. Create your Model in the Project / Folder - BlazorBoilerplate.Infrastructure.Storage DataModels - See example below
  2. Update ApplicationDbContext in Server/BlazorBoilerplate.Storage - public DbSet<NEWMODEL> NEWMODELS { get; set; }
  3. Add any foreign keys to the 'OnModelCreating(ModelBuilder modelBuilder)' function. Read EF Documentation
  4. Powershell -> cd Server/BlazorBoilerplate.Storage
  5. Build Solution
  6. Execute dotnet ef --startup-project ../BlazorBoilerplate.Server/ migrations add MIGRATION_NAME -c ApplicationDbContext --verbose --no-build --configuration Debug -o "Migrations/ApplicationDb - Replace MIGRATION_NAME
  7. Review BlazorBoilerplate.Storage Migrations/ApplicationDb for the new migration
  8. Run the project. The database changes should now be applied.

More details are found in Utils/Scripts/ef-migrations.cmd

Model Default Values

To create default values for your Model, Create a Partial Class in BlazorBoilerplate.Share.Db.Dto with the default Constructor / default values. Review the UserProfile.cs class as an example. The above partial class extends Breeze entity automatically generated (client side).


Here is an Example of a Contacts Table

using BlazorBoilerplate.Infrastructure.Storage.Permissions;
using BlazorBoilerplate.Infrastructure.Storage.DataInterfaces;
using System.ComponentModel.DataAnnotations;

#nullable enable
namespace BlazorBoilerplate.Infrastructure.Storage.DataModels
{
    [Permissions(Actions.CRUD)]
    public partial class Contact : IAuditable, ISoftDelete
    {
        [Key]
        public long Id { get; set; }

        [Required(ErrorMessage = "FieldRequired")]
        [MaxLength(128)]
        public string? LocationId { get; set; }

        [MaxLength(128)]
        public string? FirstName { get; set; }

        [MaxLength(128)]
        public string? LastName { get; set; }

        [MaxLength(128)]
        public string? Email { get; set; }

        [MaxLength(28)]
        public string? Phone { get; set; }

        [MaxLength(128)]
        public string? Address1 { get; set; }

        [MaxLength(128)]
        public string? City { get; set; }

        [MaxLength(128)]
        public string? Country { get; set; }

        [MaxLength(128)]
        public string? State { get; set; }

        [MaxLength(128)]
        public string? PostalCode { get; set; }

        [MaxLength(128)]
        public string? Website { get; set; }

        [MaxLength(128)]
        public string? Timezone { get; set; }

        [MaxLength(2048)]
        public string? Tags { get; set; }

        [MaxLength(2048)]
        public string? CustomFields { get; set; }

        public bool DND { get; set; }
    }
}

Clone this wiki locally