0

I am using Entity Framework Core 2.1.11

I have just initialized a database I will be using in my Website.

I used the Scaffold-DbContext command successfully, and it has now created all the model classes and data context class.

In the StartUp.ConfigureServices I have the following:

public void ConfigureServices(IServiceCollection services)
{
    string connection = //correct connection string used in Scaffolding
    services.AddDbContext<Models.WebsiteContext>(options => options.UseSqlServer(connection));

    services.AddMvc();
}

In my view, I have the following simple thing to test to see if it is working correctly:

@page
@model IEnumerable<Website.Models.Levels>

<table>
  @foreach (var item in Model)
  {
    <tr>
      <td>@item.Id</td>
    </tr>
  }
</table>

But unfortunately I get a cryptic error message that I'm not sure what it means:

enter image description here

To further add: when I debug the project, the constructor of the context does not get hit. I'm not sure why

1 Answer 1

1

Dependency injection is a technique for achieving inversion of control it is majorly to inject a service into the constructor of the class where it will be used.

Basically, you inject a service from an interface it implements. for instance

If you have an interface

public interface IMyInterface
{
     void doMyWork();
}

And then you had a service that implements this interface

public class MyService : IMyInterface
{    
    public void doMyWork() {
        //the work is done here
        //get to the db and fetch some good stuff the users needs
    } 
}

An you want to use DI to call this service from your controller if you are using MVC

public class HomeController : Controller
{
    private readonly IMyInterface _myinterface;

    public HomeController(IMyInterface myinterface)
    {
        _myinterface = myinterface;
    }

    public IActionResult Index()
    {
         var theWork = _myinterface.doMyWork();
    }
}

So lets say you are using a db connection as in your case your startup config needs to bind the service and the interface services.AddSingleton

public void ConfigureServices(IServiceCollection services)
{
    string connection = //correct connection string used in Scaffolding
    services.AddDbContext<Models.WebsiteContext>(options => options.UseSqlServer(connection));

    services.AddSingleton<IMyInterface, MyService>();

    services.AddMvc()
}

for more on DI for ASP.NET Core see docs

Sign up to request clarification or add additional context in comments.

8 Comments

oh so you have to have a controller?
in my ConfigureServices, I just have the services.AddMvc() what exactly does that do -- vs AddSingleton?
It is basically the way you tell your application that you want the MVC sevices available. Again you must not necessarily have a controller, injection happens in the constructor of the class/controller.
How would I access the database / display on view without controller?
You need the Controller to Route actions to the view. even if you are building with a webapi it has to be a Controller always a Controller provided you are doing MVC. you can access the database without the controller but you cannot display on the view without a controller
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.