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

Commit

Permalink
updates through 17.2
Browse files Browse the repository at this point in the history
  • Loading branch information
speudusa committed Oct 20, 2022
1 parent 7cdd8d1 commit b0ce0ac
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 20 deletions.
33 changes: 27 additions & 6 deletions src/chapters/orm-intro/accessingdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ While classes determine the structure of a table in our relational database, a *

If you want to verify what code this video starts with, check out the `db-setup <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/db-setup>`_ branch. If you want to verify what code this video ends with, check out the `persistent-data-store <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/persistent-data-store>`_ branch.

.. admonition:: Warning

The video is using an out of date syntax for registering a data store.
The text that accompanies the video has the most current syntax.

.. youtube::
:video_id: NV_Tw9sQeEQ

Expand Down Expand Up @@ -56,13 +61,20 @@ To create a persistent data store for our ``Event`` class, we can extend the cla
}
}

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 ``Event`` objects and data. We extend ``DbContext``, which will provide most of the base functionality that we need. More on this in the next section.
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
``Event`` objects and data. We extend ``DbContext``, which will provide most of the base
functionality that we need. More on this in the next section.

This extension *must* provide a property of type ``DbSet<Event>``. The ``DbSet`` class provides methods for querying sets of objects of the given type (in this case, ``Event``). In the next section, we will explore how to use these methods.
This extension *must* provide a property of type ``DbSet<Event>``.
The ``DbSet`` class provides methods for querying sets of objects of the
given type (in this case, ``Event``). In the next section, we will explore how to use these methods.

The only additional code that we need to add is a constructor that calls the constructor from the base class.
The only additional code that we need to add is a constructor that calls
the constructor from the base class.

This basic data store template can be used for any class that you want to persist in a database. Each such class will need its own data store.
This basic data store template can be used for any class that you
want to persist in a database. Each such class will need its own data store.

Registering a Data Store
^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -84,8 +96,11 @@ A persistent data store is considered a service in ASP.NET, and we can register
.. sourcecode:: csharp
:lineno-start: 29

services.AddDbContext<EventDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
var serverVersion = new MySqlServerVersion(new Version(8, 0, 29));
var defaultConnection = Configuration.GetConnectionString("DefaultConnection");

services.AddDbContext<EventDbContext>(options =>
options.UseMySql(defaultConnection, serverVersion));

Don't worry too much about the intricate details of what this code is doing. Simply note the following points:

Expand Down Expand Up @@ -131,9 +146,15 @@ However, there are two changes we need to make:

So the code sample above can be simplified to the following.

.. admonition:: Note

Need to write about why [Key] needs to be declared.
source: https://learn.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations

.. sourcecode:: csharp
:lineno-start: 16

[Key]
public int Id { get; set; }

public Event()
Expand Down
89 changes: 76 additions & 13 deletions src/chapters/orm-intro/background.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,85 @@ We now need to add a couple of NuGet packages to support our database connection
Install MySQL Dependency
~~~~~~~~~~~~~~~~~~~~~~~~

**Working with the NuGet Package Manager**

Open the NuGet Package Manager in Visual Studio:

- **Windows** - *Tools > NuGet Package Manager > Manage NuGet Packages for Solution*
- **Mac** - *Project > Manage NuGet Dependencies*

Search for ``Pomelo.EntityFrameworkCore.MySql``. Select the package and install. 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 main EntityFrameworkCore package, ``Microsoft.EntityFrameworkCore.Relational``, so it is also installed.
We will need to install the following NuGet packages:

* ``Pomelo.EntityFrameworkCore.MySql``
* ``Microsoft.EntityFrameworkCore.Relational``
* ``Microsoft.EntityFrameworkCore.Design``

Search for ``Pomelo.EntityFrameworkCore.MySql``. Select the package and install.
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 two EntityFrameworkCore packages:
``Microsoft.EntityFrameworkCore.Relational`` and
``Microsoft.EntityFrameworkCore.Design`` which must also be installed.

``Microsoft.EntityFrameworkCore.Design`` was not installed in the video, but will be required later
when we begin to migrate our data into a persistent database.

.. admonition:: Tip

You can view installed packages and their dependencies by navigating to
*Dependencies > NuGet* in the Solution Explorer (or the Solution pane on Mac)
and expanding a given package.

Verify EF Core Tools are Present
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EntityFramework Core is typically installed with Visual Studio 2022.

You can test that it has been installed by running the following in your terminal.

#. ``cd`` your way down into the project folders.
Verify your location by running the ``ls`` command. You should see all the folders within your project.

.. sourcecode:: bash

students-computer:CodingEventsDemo student$ ls
CodingEventsDemo.csproj ViewModels
Controllers Views
Data appsettings.Development.json
Models appsettings.json
Program.cs bin
Properties obj
Startup.cs wwwroot

#. When you are this level run the following command:

.. sourcecode:: bash

dotnet ef

You should see the following output:

.. tip::
.. sourcecode:: bash

students-computer:CodingEventsDemo student$ dotnet ef

_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\

You can view installed packages and their dependencies by navigating to *Dependencies > NuGet* in the Solution Explorer (or the Solution pane on Mac) and expanding a given package.
// version and command prompts to follow

Install EF Core Tools
~~~~~~~~~~~~~~~~~~~~~
Troubleshooting EF Core Tools
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In this section, "terminal" refers to the Terminal app in MacOS and Powershell in Windows (use *Tools > Command Line > Developer Powershell* to open).
If you are not able to see the EntityFrameworkCore logo,
then try the following steps to troubleshoot the issue.

In this section, "terminal" refers to the Terminal app in MacOS and
Powershell in Windows (use *Tools > Command Line > Developer Powershell* to open).

Open a terminal and run:

Expand All @@ -122,15 +186,14 @@ This command installs a set of command-line tools for working with EntityFramewo

.. admonition:: Note

This note applies to *Mac users only*.

**Mac users only**
For these tools to be accessible from the command line, they must be within your user path. Open ``~/.bash_profile`` with this command:

.. sourcecode:: bash
.. sourcecode:: bash

code ~/.bash_profile
Add the following line to the very bottom (recall that ``~`` is shorthand for your home directory, which is the directory you are in when you open a new terminal window).
code ~/.bash_profile
Add the following line to the very bottom (recall that ``~`` is shorthand for your home directory, which is the directory you are in when you open a new terminal window).

.. sourcecode:: bash

Expand All @@ -157,7 +220,7 @@ Setting the value of the ``DefaultConnection`` property using the values of the

To avoid this in the future, you can configure your ``DefaultConnection`` string to reference **environment variables**. You then hide the appropriate info by setting the environment variable's value equal to the password, for example.

See Microsoft `documentation <https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#environment-variables>`_ to learn how to keep the username and password to your database safe and secure.
See Microsoft `documentation <https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#environment-variables>`_ to learn how to keep the username and password to your database safe and secure.

Check Your Understanding
------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = 'en'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand Down

0 comments on commit b0ce0ac

Please sign in to comment.