DbContext without defining DbSets are not Supported
See original GitHub issueDescription
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:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!)
#581 introduces the usage of an intermediate service provider in the
IServiceCollectionExtensions
we needed in this issue.