From b0ce0ac572912934bed9bd49fbe40abb69509404 Mon Sep 17 00:00:00 2001 From: Courtney Frey Date: Thu, 20 Oct 2022 17:11:11 -0500 Subject: [PATCH] updates through 17.2 --- src/chapters/orm-intro/accessingdata.rst | 33 +++++++-- src/chapters/orm-intro/background.rst | 89 ++++++++++++++++++++---- src/conf.py | 2 +- 3 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/chapters/orm-intro/accessingdata.rst b/src/chapters/orm-intro/accessingdata.rst index ba6525d1..8c3a4e95 100644 --- a/src/chapters/orm-intro/accessingdata.rst +++ b/src/chapters/orm-intro/accessingdata.rst @@ -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 `_ branch. If you want to verify what code this video ends with, check out the `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 @@ -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``. 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``. +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 ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -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(options => - options.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); + var serverVersion = new MySqlServerVersion(new Version(8, 0, 29)); + var defaultConnection = Configuration.GetConnectionString("DefaultConnection"); + + services.AddDbContext(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: @@ -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() diff --git a/src/chapters/orm-intro/background.rst b/src/chapters/orm-intro/background.rst index e760d07c..18045fc7 100644 --- a/src/chapters/orm-intro/background.rst +++ b/src/chapters/orm-intro/background.rst @@ -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: @@ -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 @@ -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 `_ to learn how to keep the username and password to your database safe and secure. +See Microsoft `documentation `_ to learn how to keep the username and password to your database safe and secure. Check Your Understanding ------------------------ diff --git a/src/conf.py b/src/conf.py index b892fe36..80216bd3 100644 --- a/src/conf.py +++ b/src/conf.py @@ -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.