Page 1 of 1

Core 1.1 : Unable to resolve service for type .. ModelContext

Posted: Tue 19 Sep 2017 11:18
by jason_m
Create new ASP.NET Core Web Application Project in VS 2017
Select Web API (options: .NET Core & ASP.NET Core 1.1)
In Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 1.1.0
Install-Package Devart.Data.Oracle.EFCore.Design
Scaffold-DbContext "User Id=demo;Password=password;Data Source=XE;" Devart.Data.Oracle.Entity.EFCore -Tables PERSON -outputdir Models

Right-click Controllers-folder
Add --> New Scaffolded Item...
(Select Full Dependencies, if asked)
Select API Controller with actions, using Entity Framework
- Add
- Model Class: Person
- Data Context Class: ModelContext
- Add

To startup.cs Configure-method, add app.UseDeveloperExceptionPage();:
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseDeveloperExceptionPage();
...

Press F5 and navigate to api/Persons

--> error :
InvalidOperationException: Unable to resolve service for type 'WebApplication1.Models.ModelContext' while attempting to activate 'WebApplication1.Controllers.PersonsController'.


How to fix this?

Re: Core 1.1 : Unable to resolve service for type .. ModelContext

Posted: Wed 20 Sep 2017 06:09
by jason_m
Solved: Add:
services.AddDbContext<ModelContext>(options => options.UseOracle(Configuration["ORACLE_CONNECTION_STRING"]));
To ConfigureServices-method in Startup.cs

Re: Core 1.1 : Unable to resolve service for type .. ModelContext

Posted: Fri 22 Sep 2017 12:00
by Pinturiccio
We are glad to hear that the issue is solved. Feel free to contact us if you have any further questions.