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?
Core 1.1 : Unable to resolve service for type .. ModelContext
Re: Core 1.1 : Unable to resolve service for type .. ModelContext
Solved: Add:
services.AddDbContext<ModelContext>(options => options.UseOracle(Configuration["ORACLE_CONNECTION_STRING"]));
To ConfigureServices-method in Startup.cs
services.AddDbContext<ModelContext>(options => options.UseOracle(Configuration["ORACLE_CONNECTION_STRING"]));
To ConfigureServices-method in Startup.cs
-
- Devart Team
- Posts: 2420
- Joined: Wed 02 Nov 2011 09:44
Re: Core 1.1 : Unable to resolve service for type .. ModelContext
We are glad to hear that the issue is solved. Feel free to contact us if you have any further questions.