Adding a license manager
Hi, I added a new license module in Infrastucture project on below. But I've got an error like this:
One or more errors occurred. (The entity type 'IdentityUserLogin
I'm using in my LicenseService the this file and LicenseService is injected to UserService.
This my DbContext file :
using Finbuckle.MultiTenant.Abstractions;
using Finbuckle.MultiTenant.EntityFrameworkCore;
using FSH.Framework.Core.Persistence;
using FSH.Framework.Infrastructure.Tenant;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace FSH.Framework.Infrastructure.License.Persistence;
public class LicenseDbContext : MultiTenantDbContext
{
private readonly DatabaseOptions _settings;
private new FshTenantInfo TenantInfo { get; set; }
public LicenseDbContext(IMultiTenantContextAccessor<FshTenantInfo> multiTenantContextAccessor,
DbContextOptions<LicenseDbContext> options, IOptions<DatabaseOptions> settings) : base(multiTenantContextAccessor, options)
{
TenantInfo = multiTenantContextAccessor.MultiTenantContext.TenantInfo!;
_settings = settings.Value;
}
public DbSet<LicenseItem> Licenses { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(LicenseDbContext).Assembly);
modelBuilder.Entity<LicenseItem>().ToTable("license", "license");
}
}
This file is my initializer
using FSH.Framework.Core.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FSH.Framework.Infrastructure.License.Persistence;
public class LicenseDbInitializer(
ILogger<LicenseDbContext> logger,
LicenseDbContext context) : IDbInitializer
{
public async Task MigrateAsync(CancellationToken cancellationToken)
{
var item = context.TenantInfo?.Identifier;
if ((await context.Database.GetPendingMigrationsAsync(cancellationToken).ConfigureAwait(false)).Any())
{
await context.Database.MigrateAsync(cancellationToken).ConfigureAwait(false);
logger.LogInformation("[{Tenant}] applied database migrations for Lisence module", context.TenantInfo?.Identifier);
}
}
public async Task SeedAsync(CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(context.TenantInfo?.Id))
{
return;
}
var license = new LicenseItem
{
Id = Guid.NewGuid().ToString(),
TenantId = context.TenantInfo?.Id,
UserLimit = 5,
AdminLimit = 1,
LicenseName = "Trial License for 20 Days",
ExpiresAt = DateTimeOffset.Now.AddDays(20),
DiskSpaceLimit = 100,
IsTrial = true,
IsActivated = true
};
_ = await context.Licenses.AddAsync(license, cancellationToken);
_ = await context.SaveChangesAsync();
}
}
My Entity:
namespace FSH.Framework.Infrastructure.License;
public class LicenseItem
{
public string Id { get; set; } = default!;
public string LicenseName { get; set; }
public int AdminLimit { get; set; }
public int UserLimit { get; set; }
public string TenantId { get; set; } = default!;
public int DiskSpaceLimit { get; set; }
public bool IsTrial { get; set; }
public bool IsActivated { get; set; }
public DateTimeOffset ExpiresAt { get; set; }
}
What are your suggestions? @justSteve @maxiar @pashaie @fretje
Regards
I think your issue is
modelBuilder.ApplyConfigurationsFromAssembly(typeof(LicenseDbContext).Assembly);
this will add all configurations from that assembly, so probably also some configurations you don't want to add... I think you only need
modelBuilder.Entity<LicenseItem>().ToTable("license", "license");
OTOH, you should probably just create a LicenseItemEntityTypeConfiguration that implements IEntityTypeConfiguration<LicenseItem>, and put the configuration there. That should then automatically get picked up by the ApplicationDbContext... No need to create another DbContext.
Hmm... You can disregard that last comment wrt the applicationdbcontext... I didn't see the split up in modules yet... But the main problem stays the same... you probably put your module's dbcontext in the same project like another module, so then you can't use "ApplyConfigurationsFromAssembly"... If you do want to use "ApplyConfigurationsFromAssembly", you need to put your module into its own assembly (project). But as you don't use "EntityTypeConfigurations" in your module, that call is simply not necessary.
Thank you @fretje. I removed ApplyConfigurationsFromAssembly line and it done :) Thank you for your interest