Skip to content

DbContext without defining DbSets are not Supported #550

Closed
@ahmadalli

Description

@ahmadalli

Description

this is my DbSet:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        Type[] types = typeof(AppDbContext).GetTypeInfo().Assembly.GetTypes();
        IEnumerable<Type> typesToRegister = types
            .Where(type => type.GetInterfaces()
                .Where(i => i.IsGenericType)
                .Any(i => i.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>)));

        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            addConfiguration(modelBuilder, configurationInstance);
        }

        base.OnModelCreating(modelBuilder);
    }

    private static void addConfiguration<TEntity>(ModelBuilder modelBuilder, IEntityTypeConfiguration<TEntity> configuration)
        where TEntity : class
    {
        configuration.Configure(modelBuilder.Entity<TEntity>());
    }
}

and I configure each model using

#region configurations
public class Configuration : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {
        builder.ToTable(nameof(Person).Pluralize());
        builder.HasKey(x => x.Code);

        builder.Ignore(x => x.Id).Ignore(x => x.StringId);
    }
}
#endregion

inside my model.

but when I try to load PersonController, 'm getting this error:

A resource has not been properly defined for type 'Person'. Ensure it has been registered on the ResourceGraph.

Environment

  • JsonApiDotNetCore Version: 3.1.0
  • Other Relevant Package Versions:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions