I am use DI in my controller class as documented here. The object I am passing with DI has to be used as dbset.
public class ValuesController : ControllerBase
{
private readonly RPContext _context;
private IResource _resource;
public ValuesController(RPContext context, IResource resource)
{
_context = context;
_resource = resource;
}
[HttpPost("{id}/{resourceName}")]
public void Post([FromRoute] string id, [FromRoute] string resourceName, [FromBody] JObject Request)
{
_resource.Id = id;
_resource.Name = resourceName;
_resource.Location = (string)Request["location"];
_context.Resources.Add(_resource);
_context.SaveChangesAsync();
}
I cannot use "_resource" in the above code with the Add method of Dbcontext object because it is an interface and not of class type. How else should I be doing this if I don't want to create new objects in my controller class?
1.The controller inherit toControllerBaseinstead ofController.2.Using asynchronous task but returningvoid.3.Try to cast an interface to a class... I suggest you to read the documentation careful and follow step-by-step to understand how it works. Or just download the project and run (via the link you provide).Controller, not justControllerBase(I'm sure). And the question about the difference(s) may be in another post. I said that because: when I create new controller in my project (.net core 2.1), it inherits fromController.