Skip to content
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

Improve Error Message: Resolving unregistered service from internal service provider #5500

Closed
rowanmiller opened this issue May 24, 2016 · 0 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@rowanmiller
Copy link
Contributor

Currently we assume that resolving an unregistered service means you haven't configured a provider:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

But it is particularly confusing if you try to resolve a context from the internal service provider (not a super weird thing to assume might work):

public static void MyExtensionMethod<TEntity>(this DbSet<TEntity> set)
{
    var context = set.GetService<DbContext>();
}

We should update the message so say something like "Could not resolve '', this may be because...".

@rowanmiller rowanmiller added this to the 1.0.0 milestone May 26, 2016
@rowanmiller rowanmiller self-assigned this May 26, 2016
rowanmiller added a commit that referenced this issue May 31, 2016
If you call an extension method such as
`DbContext.Database.GetDbConnection()` and we fail to resolve the
connection from DI, then we assumed this was because you did not
register a provider. This is often the case, but it's a misleading error
message if you are just trying to resolve some service that is not
present. Adding some pre-amble to this exception to explain this.

Note we still throw the existing message if we are trying to locate the
configured provider and there is none
(see `DatabaseProviderSelector.SelectServices()`) since we are sure that
is the problem in that case.

Fixes #5500
rowanmiller added a commit that referenced this issue Jun 1, 2016
If you call an extension method such as
`DbContext.Database.GetDbConnection()` and we fail to resolve the
connection from DI, then we assumed this was because you did not
register a provider. This is often the case, but it's a misleading error
message if you are just trying to resolve some service that is not
present. Adding some pre-amble to this exception to explain this.

Note we still throw the existing message if we are trying to locate the
configured provider and there is none
(see `DatabaseProviderSelector.SelectServices()`) since we are sure that
is the problem in that case.

Fixes #5500
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants