-
-
Notifications
You must be signed in to change notification settings - Fork 158
DbContext without defining DbSets are not Supported #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As the error states, you will ned to register it to the resourcegraph https://json-api-dotnet.github.io/JsonApiDotNetCore/usage/resource-graph.html |
the point is I'm using entity framework discovery method but it doesn't support models built by model builder instead of dbset |
Ah. That might be true, as we expect there to be a DbSet Available, I don't know if this is an easy fix at the moment. If you have time for investigation you are more than welcome to add a pull request as this does seem like an OK things to support. I don't have much need for this feature as I explicitly define my models. If we get more requests in this area we can consider devoting some time to it. |
@wisepotato could you please show me some initial point to investigate the issue? |
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: JsonApiDotNetCore/src/JsonApiDotNetCore/Services/JsonApiContext.cs Lines 75 to 77 in 8cca15a
But in our version, that we're working on for V4, the New version is here: , as you see the function that throws your errors has been made obsolete. (and for good reason!) |
I found it but I'm not sure how can I fix it. we need an instance of dbContext here: JsonApiDotNetCore/src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs Lines 235 to 262 in ec4e76e
then we can change public IResourceGraphBuilder AddDbContext<T>() where T : DbContext
{
_usesDbContext = true;
var contextType = typeof(T);
var dbContext = // get the instance of T here
var types = contextType.GetProperties()
.Where(t => t.PropertyType.IsGenericType)
.Where(t => t.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>))
.Select(t => t.PropertyType.GetGenericArguments()[0])
.Union(dbContext.Model.GetEntityTypes().Select(et => et.ClrType))
.Distinct()
.ToList();
foreach (var entityType in types)
{
AssertEntityIsNotAlreadyDefined(entityType);
var (isJsonApiResource, idType) = GetIdType(entityType);
if (!isJsonApiResource) continue;
var resourceName = _resourceNameFormatter.FormatResourceName(entityType);
_entities.Add(GetEntity(resourceName, entityType, idType));
}
return this;
} |
I mean i cant validate if this works, but if you're free to add this to master via a pull request, i welcome it. |
the problem is I don't know how can I get the instance of |
P.S. I've added the properties for now but it'd be good if we solved that issue too |
Hi @ahmadalli, thanks for opening up this issue. It seems like a useful thing to support. Like you mentioned, we need to access the service provider to get an instance of the DbContext, but at the time of calling
Note that we currently have very limited capacity to work on this. As @wisepotato mentioned, we are currently prioritising #504, which is required for JsonApiDotNetCore to be truly production ready (due to testability). To advance with your project, I think manually constructing the resource graph If you're interested in really tackling this issue yourself, hit me up in our gitter channel! |
#581 introduces the usage of an intermediate service provider in the |
Description
this is my DbSet:
and I configure each model using
inside my model.
but when I try to load
PersonController
, 'm getting this error:Environment
The text was updated successfully, but these errors were encountered: