question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DbContext without defining DbSets are not Supported

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wisepotatocommented, Aug 27, 2019

To be honest? Wait a week or 2, or explicitely state your models for now (if thats possible for you). We are working on decoupling the JsonApiContext (where you’re getting your error) and making it more testable and (hint hint) more extendable for different uses.

You’re getting the error here:

https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/8cca15a5ba42f8543ae937a2dcc867ee6cd303ff/src/JsonApiDotNetCore/Services/JsonApiContext.cs#L75-L77

But in our version, that we’re working on for V4, the _controllerContext is non-existent. So what I suggest is: either help and see if you can make a basic testable example project (which helps us immensely) and do that in the branch .

New version is here:

https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/feat/context-decoupling/src/JsonApiDotNetCore/Services/JsonApiContext.cs#L80

, as you see the function that throws your errors has been made obsolete. (and for good reason!)

0reactions
maureicommented, Oct 17, 2019

#581 introduces the usage of an intermediate service provider in the IServiceCollectionExtensions we needed in this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use DbContext when there is no DbSet<> inside ...
I'm using Entity Framework Code-First and this is my DbContext. As you see there is nothing about DbSet<> properties and all of that...
Read more >
Defining DbSets - EF6
In this article. DbContext with DbSet properties; DbContext with IDbSet properties; DbContext with read-only set properties.
Read more >
Best Practices in Using the DbContext in EF Core
The DbContext is a singleton class that represents the gateway to all data access, and therefore should not be instantiated more than once....
Read more >
DbSet in EF 6
The context class (derived from DbContext ) must include the DbSet type properties for the entities which map to database tables and views....
Read more >
Querying data via the DbSet
In EF Core, you can return non-entity types from your LINQ queries. Non-entity types are classes that are not mapped to any database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found