DEV Community

.Net Core Shared Link Returning Null?

mitchelln11 on June 15, 2020

I am trying to convert a .NET Framework web application to a .NET Core web application. I did start a new project, so I'm copying over info. In th...
Collapse
 
mitchelln11 profile image
mitchelln11

So I did get it working for anybody else that runs across this.

Previously, the following would work in .NET Framework: (On the controller - HikerController in my case)

private ApplicationDbContext _context = new ApplicationDbContext();

Now in .NET Core, you need a constructor; starts a similar way:

private ApplicationDbContext _context;
public HikerController(ApplicationDbContext context)
{
  _context = context
}

Then you would reference database info something like this: _context.Hikers....
I have the following to display all of my hikers:

        public ActionResult Index()
        {
            return View(_context.Hikers.ToList());
        }

*If you don't use a constructor and pass in the ApplicationDbContext, you will get the following:

An unhandled exception occurred while processing the request.
InvalidOperationException: 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
Collapse
 
ryanmaffey profile image
Ryan Maffey

It’s a little hard to tell but the only thing I can think of is that your view is expecting a model but you’re not providing one.

Collapse
 
mitchelln11 profile image
mitchelln11 • Edited

What I have for the Hikers view, which should display all of the hikers. Shouldn't that first line do the trick?

@model IEnumerable<walkinthepark.Models.Hiker>

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StreetAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.City)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.State)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Latitude)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Longitude)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StreetAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.State)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Latitude)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Longitude)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
}
    </tbody>
</table>

If I set up an empty view, and link to it, it gets there just fine. Once I add database info, then it fails.

UPDATE:
So I have added this since, but I am faced with another error.

private ApplicationDbContext db = new ApplicationDbContext();

        public ActionResult Index()
        {
            return View(db.Hikers);
        }

Error reads:

An unhandled exception occurred while processing the request.
InvalidOperationException: 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
Collapse
 
ryanmaffey profile image
Ryan Maffey

Glad you fixed the issue with the model. Unfortunately I'm not going to be able to help with the latter issue but I'd imagine the best place to start is the Entity Framework Core docs: docs.microsoft.com/en-us/ef/core/