fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.19k stars 1.56k forks source link

[BUG] Adding Migrations To MySQL and PostgreSQL #235

Closed codebycinar closed 2 years ago

codebycinar commented 2 years ago

Hi, thanks for awesome project template. I'm testing for using this one of the my projects.

I can add migration with MSSQL db but I can't add migrations to MySQL and PostgreSQL. Both gives me this error. I added some entities, dbsets and relations. And overrided OnModelCreating like this:

protected override void OnModelCreating(ModelBuilder modelBuilder) { if (Database.IsRelational()) { modelBuilder.Entity<OperatorResourceGroup>() .HasKey(bc => new { bc.OperatorId, bc.ResourceGroupId }); modelBuilder.Entity<OperatorResourceGroup>() .HasOne(bc => bc.Operator) .WithMany(b => b.ResourceGroups) .HasForeignKey(bc => bc.OperatorId); modelBuilder.Entity<OperatorResourceGroup>() .HasOne(bc => bc.ResourceGroup) .WithMany(c => c.Operators) .HasForeignKey(bc => bc.ResourceGroupId); modelBuilder.Entity<WorkOrder>() .HasOne(g => g.PlannedMachine) .WithMany(t => t.PlannedWorkOrders) .HasForeignKey(t => t.PlannedMachineId) .HasPrincipalKey(t => t.Id); modelBuilder.Entity<WorkOrder>() .HasOne(g => g.ActualMachine) .WithMany(t => t.ActualWorkOrders) .HasForeignKey(t => t.ActualMachineId).OnDelete(DeleteBehavior.NoAction) .HasPrincipalKey(t => t.Id); modelBuilder.Entity<WorkOrder>() .HasOne(g => g.PlannedOperator) .WithMany(t => t.PlannedWorkOrders) .HasForeignKey(t => t.PlannedOperatorId) .HasPrincipalKey(t => t.Id); modelBuilder.Entity<WorkOrder>() .HasOne(g => g.ActualOperator) .WithMany(t => t.ActualWorkOrders) .HasForeignKey(t => t.ActualOperatorId).OnDelete(DeleteBehavior.NoAction) .HasPrincipalKey(t => t.Id); } base.OnModelCreating(modelBuilder); }

[12:06:00 INF] Server Booting Up... [12:06:00 INF] Hangfire: Current Storage Provider : postgresql [12:06:00 INF] For more HangFire storage, visit https://www.hangfire.io/extensions.html [12:06:00 INF] Current DB Provider : postgresql [12:06:04 INF] Connection to ........'s Database Succeeded. System.InvalidOperationException: Unable to build IHost at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost() at Microsoft.Extensions.Hosting.HostFactoryResolver.<>cDisplayClass8_0.b0(String[] args) at Microsoft.Extensions.Hosting.HostFactoryResolver.<>cDisplayClass11_0.b3(String[] args) at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args) An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Unable to build IHost No application service provider was found. Finding DbContext classes in the project... Found DbContext 'TenantManagementDbContext'. Found DbContext 'ApplicationDbContext'. Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' while attempting to activate 'Infrastructure.Persistence.Contexts.ApplicationDbContext'.

boradakash commented 2 years ago

I am also getting the same error for MSSQL:

Build started... Build succeeded. [15:24:36 INF] Server Booting Up... [15:24:39 INF] Hangfire: Current Storage Provider : mssql [15:24:39 INF] For more HangFire storage, visit https://www.hangfire.io/extensions.html [15:24:39 INF] Current DB Provider : mssql An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Unable to build IHost Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Temporary solution I have found is deleting all old migrations and adding new migration will fix it.

iammukeshm commented 2 years ago

You are using the package? Or using the latest code?

boradakash commented 2 years ago

Not using package manager console but using dotnet-ef tool

boradakash commented 2 years ago

Using this command: dotnet ef migrations add addedInitialTables --project .././Migrators/Migrators.MSSQL/ --context ApplicationDbContext -o Migrations/Application

boradakash commented 2 years ago

Didn't have the latest code but pull the changes when 0.0.5rc released

boradakash commented 2 years ago

I wonder it could be related to namespace but couldn't figure it out where it could be. Will keep you posted if I will find anything.

codebycinar commented 2 years ago

I can Migrate on MSSQL if I run application with Initial Root migrations first. After that i can add-migration with my entities. But this is not working with MySQL and PostgreSQL.

iammukeshm commented 2 years ago

I have readded all the migrations in the latest code, can someone check if the issues exists?

boradakash commented 2 years ago

Sure thanks, will check.

boradakash commented 2 years ago

It would be great if you can guide me how to update it? I've tried this from this SO answer: https://stackoverflow.com/questions/56577184/github-pull-changes-from-a-template-repository But it has created conflict in 250+ file (all file's namespaces conflict and few others), is there any other way to update it smoothly?

fretje commented 2 years ago

Yeah a lot has changed with regards to the project structure, nullability, startup, ...

I guess the easiest will be to create a new project with the latest version and copy over your custom code. Sorry for that :-s

boradakash commented 2 years ago

I recently done that(creating a new project and copy over my changes) when 0.0.5rc released. Now I am trying to get the latest changes as Mukesh suggested to try out with latest changes but getting lots of conflict. I might be doing something wrong though. But do I need to start from fresh project every time when there are new changes?

fretje commented 2 years ago

No it's not you, the changes I was talking about all were done after the 0.0.5 release.

There were some really big ones this time. So that's why starting from fresh is the recommended way. It should get easier in the future once the main structure stabilizes ;-)

dsoltesz commented 2 years ago

I would rather see things broken out better into projects as opposed to only a few projects with lots of folders…once you start working on a real application this will get bulky real quick data project, domain project, services project etc


From: fretje @.> Sent: Saturday, December 4, 2021 9:56:11 AM To: fullstackhero/dotnet-webapi-boilerplate @.> Cc: Subscribed @.***> Subject: Re: [fullstackhero/dotnet-webapi-boilerplate] [BUG] Adding Migrations To MySQL and PostgreSQL (Issue #235)

No it's not you, the changes I was talking about all were done after the 0.0.5 release.

There were some really big ones this time. So that's why starting from fresh is the recommended way. It should get easier in the future once the main structure stabilizes ;-)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/fullstackhero/dotnet-webapi-boilerplate/issues/235#issuecomment-986040583, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AALFFSJD3MFPHSL3VM5HMQLUPITYXANCNFSM5JLLWARQ.

fretje commented 2 years ago

There are already the separate projects for Application, Domain and Shared.DTOs.

I think it's something personal as I already don't really see the point of the separate Shard.DTOs project, and prefer them together with the code that uses them.

When adding application features, the infrastructure project shouldn't really change. Most changes happen in those "Application" and "Domain" projects. You could make separate ones of those for individual features of the application (vertical slices if you will) but that's up to the person using the template and will be different depending on the needs of the project. The template itself only implements one example feature, namely the catalog feature. I think it would be a bit overkill to already add separate projects for one feature?

Anyway, there's been done a lot of work wrt the general structure lately, but there's definitely more room to make it better/easier understandable, and any feedback is welcome! Even better if you show an example (using a PR) and it will definitely get looked into!

codebycinar commented 2 years ago

I have readded all the migrations in the latest code, can someone check if the issues exists?

I updated the code. Root migrations are applying successfully on 3 db providers. But after added some relational entities MySQL and PosgreSQL migrations failing. Just MSSQL working for me for now.

[01:12:38 INF] Server Booting Up... [01:12:38 INF] Hangfire: Current Storage Provider : postgresql [01:12:38 INF] For more Hangfire storage, visit https://www.hangfire.io/extensions.html [01:12:38 INF] Current DB Provider : postgresql [01:12:42 INF] Connection to mmes's Database Succeeded. System.InvalidOperationException: Unable to build IHost at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost() at Microsoft.Extensions.Hosting.HostFactoryResolver.<>cDisplayClass8_0.b0(String[] args) at Microsoft.Extensions.Hosting.HostFactoryResolver.<>cDisplayClass11_0.b3(String[] args) at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args) An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Unable to build IHost No application service provider was found. Finding DbContext classes in the project... Found DbContext 'TenantManagementDbContext'. Found DbContext 'ApplicationDbContext'. Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' while attempting to activate 'Infrastructure.Persistence.Contexts.ApplicationDbContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>cDisplayClass21_4.b13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>cDisplayClass21_4.b13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

iammukeshm commented 2 years ago

@codebycinar So

  1. you pulled latest code
  2. ran the application
  3. added a new entity / relation
  4. added migrations for PostgreSQL / MySQL using dotnet ef migrations add <CommitMessage> --project .././Migrators/Migrators.<Provider>/ --context ApplicationDbContext -o Migrations/Application
  5. CLI throws an exception.

Are these the steps to replicate this issue?

iammukeshm commented 2 years ago

I just tried to do same exact thing.

  1. Took latest code,
  2. Changed the DB and Hangfire connection strings to PostgreSQL Providers.
  3. Ran the application once. DB is created on PostgreSQL
  4. Added a new property to Product Class, named QRCode.
  5. Added Migrations. Screenshot below.

image

  1. Migrations added as expected.
  2. Ran the application. Noticed that the application automatically applied the latest migrations and updated the DB Schema.
  3. Checked the DB via PGAdmin. Can see the newly added column.

image

Are these the steps you followed as well? Because it works as expected for me.

boradakash commented 2 years ago

Weird, I just tried with the following steps(with new fresh project but getting same error)

  1. dotnet new --install FullStackHero.WebAPI.Boilerplate::0.0.5-rc
  2. dotnet new fsh-api -o FSH.Starter
  3. Added new folder(named Project) in domain project and added new class there called Template.cs
  4. Template.cs
    
    using FSH.Starter.Domain.Common.Contracts;
    using FSH.Starter.Domain.Contracts;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

namespace FSH.Starter.Domain.Project;

public class Template : AuditableEntity, IMustHaveTenant { public string Name { get; private set; } public string Description { get; private set; } public string Tenant { get; set; }

public Template(string name, string description)
{
    Name = name;
    Description = description;
}

}

5. Added following line in ApplicationDbContext
`public DbSet<Template> Templates { get; set; }`

6. Run migration command:
`H:\FSH.Starter\src\Host>dotnet ef migrations add addedTemplateTable --project .././Migrators/Migrators.MSSQL/ --context ApplicationDbContext -o Migrations/Application`

and it threw following error:

Build started... Build succeeded. [13:30:09 INF] Server Booting Up... [13:30:11 INF] Hangfire: Current Storage Provider : mssql [13:30:11 INF] For more HangFire storage, visit https://www.hangfire.io/extensions.html [13:30:11 INF] Current DB Provider : mssql An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Unable to build IHost Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728



I wonder the the only difference is change in ApplicationDbContext file. so just wondering it might be throwing only when there's a change in ApplicationDbContext file
iammukeshm commented 2 years ago

@boradakash I tried the same thing, but not with Nuget Package.

I suggest you delete all the files from Migrators.MySQL folder and re-run the entire migrations with a fresh database.

dotnet ef migrations add Initial --project .././Migrators/Migrators.MSSQL/ --context ApplicationDbContext -o Migrations/Application

dotnet ef migrations add Initial --project .././Migrators/Migrators.MSSQL/ --context TenantManagementDbContext -o Migrations/Root

Let me know if it worked.

image

iammukeshm commented 2 years ago

But, I just tried to use 0.0.5 and was not able to replicate this issue there! Make sure your environment is setup right and you don't have some kind of port clashes.