Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
dotnet 6 updates added
Browse files Browse the repository at this point in the history
updates added to work with dotnet 6
  • Loading branch information
speudusa authored Dec 1, 2022
2 parents b337117 + 21eabaf commit da8e5a1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
32 changes: 16 additions & 16 deletions src/chapters/auth/scaffolding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ Inside the project directory, run the following command:

When you run this command, the output may look something like the following:

.. sourcecode:: guess
.. sourcecode:: bash
:linenos:

.NET SDK (reflecting any global.json):
Version: 6.0.403
Commit: 2bc18bf292
.NET SDK (reflecting any global.json):
Version: 6.0.403
Commit: 2bc18bf292

Runtime Environment:
OS Name: Mac OS X
OS Version: 12.4
OS Platform: Darwin
RID: osx.12-x64
Base Path: /usr/local/share/dotnet/sdk/6.0.403/
Runtime Environment:
OS Name: Mac OS X
OS Version: 12.4
OS Platform: Darwin
RID: osx.12-x64
Base Path: /usr/local/share/dotnet/sdk/6.0.403/

global.json file:
Not found
global.json file:
Not found

Host:
Version: 6.0.11
Architecture: x64
Commit: 943474ca16
Host:
Version: 6.0.11
Architecture: x64
Commit: 943474ca16

If the .NET Core SDK listed on line 2 does not match the SDK specified in your ``csproj`` file, you may need to open up your ``global.json`` and edit it so that the SDK used by the project matches.

Expand Down
28 changes: 14 additions & 14 deletions src/chapters/orm-intro/accessingdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ To create a persistent data store for our ``Event`` class, we can extend the cla
.. sourcecode:: csharp
:linenos:

using CodingEventsDemo.Models;
using Microsoft.EntityFrameworkCore;
using CodingEventsDemo.Models;
using Microsoft.EntityFrameworkCore;

namespace CodingEventsDemo.Data
{
public class EventDbContext : DbContext
namespace CodingEventsDemo.Data
{
public DbSet<Event> Events { get; set; }

public EventDbContext(DbContextOptions<EventDbContext> options)
: base(options)
public class EventDbContext : DbContext
{
public DbSet<Event> Events { get; set; }

public EventDbContext(DbContextOptions<EventDbContext> options)
: base(options)
{
}
}
}
}

This new class is placed in the ``Data`` directory and namespace.
By convention, we name it ``EventDbContext`` since it is going to be used to work with
Expand Down Expand Up @@ -101,10 +101,10 @@ Open up ``Startup.cs`` and find the ``ConfigureServices`` method. By default, it
.. sourcecode:: csharp
:lineno-start: 26

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}

A persistent data store is considered a service in ASP.NET,
and we can register this service by adding the following code to ``ConfigureServices``.
Expand Down
12 changes: 6 additions & 6 deletions src/chapters/orm-intro/background.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,21 @@ Open the NuGet Package Manager in Visual Studio:
- **Mac** - *Project > Manage NuGet Dependencies*

Search for for all of the packages listed below. Select the package and install.
Note the version numbers. When using the package manager, you should be able to select a version.
Match the versions provided below.

When installing these packages, make sure that the versions are the same as the .NET Core version your project is using.
You can confirm this is the case by reviewing the code in your csproj file.

We will need to install the following NuGet packages:

* ``Pomelo.EntityFrameworkCore.MySql`` Version 6.0.2
* ``Pomelo.EntityFrameworkCore.MySql``
This dependency provides code that is able to connect to a MySQL database
from within an ASP.NET Core application using EF. Note that this package
itself depends on the following EF packages:

* ``Microsoft.EntityFrameworkCore.Relational`` Version 6.0.11
* ``Microsoft.EntityFrameworkCore.Relational``
This is a mapping framework that automates access and storage of data in your project's database.

* ``Microsoft.EntityFrameworkCore.Design`` Version 6.0.11
* ``Microsoft.EntityFrameworkCore.Design``
This helps manage data migrations and the design-time logic.
**Note:** This was not installed in the video above.
If you do not install it, Entity Framework Core will print an error message asking you to install it.
Expand Down Expand Up @@ -176,7 +177,6 @@ You can test that it has been installed by running the following in your termina
|___||_| / \\\/\\

Entity Framework Core .NET Command-line Tools 6.0.X


.. admonition:: Note

Expand Down
74 changes: 37 additions & 37 deletions src/chapters/orm-intro/data-stores.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ To access data from the database in a controller, we'll need an instance of ``Ev
.. sourcecode:: csharp
:lineno-start: 17

private EventDbContext context;
private EventDbContext context;

public EventsController(EventDbContext dbContext)
{
context = dbContext;
}
public EventsController(EventDbContext dbContext)
{
context = dbContext;
}

We can now reference ``context`` anywhere within our controller in order to query the database.

Expand All @@ -57,12 +57,12 @@ The first such usage is in the ``Index`` method, which displays a listing of all
.. sourcecode:: csharp
:lineno-start: 25

public IActionResult Index()
{
List<Event> events = context.Events.ToList();
public IActionResult Index()
{
List<Event> events = context.Events.ToList();

return View(events);
}
return View(events);
}

We use the ``ToList`` method of ``context.Events`` (recall that this property is a ``DbSet``) to fetch a list of *all* ``Event`` objects stored in the database.

Expand All @@ -71,8 +71,8 @@ Our next usage of ``EventData`` that needs to be replaced is in the ``Add`` meth
.. sourcecode:: csharp
:lineno-start: 53

context.Events.Add(newEvent);
context.SaveChanges();
context.Events.Add(newEvent);
context.SaveChanges();

Line 53 adds the object to ``context.Events``, which only stores it within that ``DbSet`` object. For this change to be pushed to the database, we must also call ``context.SaveChanges()``.

Expand All @@ -87,28 +87,28 @@ The next usage of ``EventData`` is in the ``Delete`` method that handles GET req
.. sourcecode:: csharp
:lineno-start: 62

public IActionResult Delete()
{
ViewBag.events = context.Events.ToList();
public IActionResult Delete()
{
ViewBag.events = context.Events.ToList();

return View();
}
return View();
}

The final usage of ``EventData`` is in the ``Delete`` method that handles POST requests. That method currently looks like this:

.. sourcecode:: csharp
:lineno-start: 61

[HttpPost]
public IActionResult Delete(int[] eventIds)
{
foreach (int eventId in eventIds)
[HttpPost]
public IActionResult Delete(int[] eventIds)
{
EventData.Remove(eventId);
}
foreach (int eventId in eventIds)
{
EventData.Remove(eventId);
}

return Redirect("/Events");
}
return Redirect("/Events");
}

The method takes in an array of IDs corresponding to objects that should be deleted. It then loops through the array and deletes the corresponding objects one-by-one.

Expand All @@ -117,8 +117,8 @@ Line 66 can be replaced with the following:
.. sourcecode:: csharp
:lineno-start: 66

Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);
Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);

The first line searches ``context.Events`` for an object with the given ID using its ``Find`` method. It returns the given object or ``null`` (if none is found). We can then delete the object by calling the ``Remove`` method of ``context.Events`` and passing in the object we want to delete.

Expand All @@ -129,19 +129,19 @@ Our final refactored method looks like this:
.. sourcecode:: csharp
:lineno-start: 70

[HttpPost]
public IActionResult Delete(int[] eventIds)
{
foreach (int eventId in eventIds)
[HttpPost]
public IActionResult Delete(int[] eventIds)
{
Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);
}
foreach (int eventId in eventIds)
{
Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);
}

context.SaveChanges();
context.SaveChanges();

return Redirect("/Events");
}
return Redirect("/Events");
}

Now that we are no longer using ``EventData``, we can delete it from our application. And as always, be sure to start your app and test after refactoring.

Expand Down
6 changes: 3 additions & 3 deletions src/chapters/orm-intro/studio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ To do so, open up ``_Layout.cshtml`` and scroll down to approximately line 24 wh
.. sourcecode:: guess
:lineno-start: 24

<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Events" asp-action="Add">Add</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Events" asp-action="Add">Add</a>
</li>

Using this code as a template, add links to the ``Events/Index.cshtml``, ``EventCategory/Create.cshtml``, and ``EventCategory/Index.cshtml`` views.

Expand Down

0 comments on commit da8e5a1

Please sign in to comment.