dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

Adding a license manager

Open umutaktas opened this issue 2 years ago • 3 comments

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' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.)

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

umutaktas avatar Nov 24 '24 22:11 umutaktas

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.

fretje avatar Dec 02 '24 09:12 fretje

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.

fretje avatar Dec 02 '24 09:12 fretje

Thank you @fretje. I removed ApplyConfigurationsFromAssembly line and it done :) Thank you for your interest

umutaktas avatar Dec 02 '24 13:12 umutaktas