Giter Site home page Giter Site logo

pluralsight-efcore-getting-started's Introduction

Entity Framework Core: Getting Started

This is based on the Pluralsight course, Entity Framework Core: Getting Started, but modified slightly to work more nicely with .NET 6 and in IDE/editors other than Visual Studio, in particular VS Code.

Rather than attaching a database to the project directly, which would be relatively normal in Visual Studio, this connects to a local MS SQL/Postgres server which is handled by Docker via Docker Compose (see notes).

Requirements

  • .NET 6.0 SDK
  • The Entity Framework tool should be installed globall for .NET with dotnet tool install --global dotnet-ef
  • Docker (configured to be able to run Linux containers)

Getting Started

For managing the database, there are a few commands which are useful to have to hand:

  • To start up the database server the first time and watch the logs:
    docker compose up
    
  • To start up the database server the first time and not watch the logs:
    docker compose up -d
    
  • After the database server has been started once, but is currently stopped and you want to restart it:
    docker compose start
    
  • If the database server is currently running and you want to stop it:
    docker compose stop
    
  • If you want to tear down the database server (i.e. stop it if it's running and remove it entirely):
    docker compose down
    

Using dotnet commands instead of the Package Manager Console

As we're not using Visual Studio, it makes sense to use the dotnet CLI instead of the Package Manager Console to handle Entity Framework commands. Here are a few examples of how that changes:

Package Manager Console dotnet Description
add-migration <name> dotnet ef migrations add <name> Creates a new migration in Entity Framework
update-database dotnet ef database update Runs all migrations which have not been executed against the database
get-migration dotnet ef migrations list List all migrations and their status

You can view more of these commands with the dotnet ef command.

Notes

  • There is a .env file in the root directory which contains the environment variables for the MS SQL Server, this may not normally be under source control as it contains secrets (or it may be one of many environment variables files, of which one or more may not be under source control for similar reasons).
  • Currently, the connection string is hardcoded in the SamuraiApp.Data project, in the SamuraiContext.cs file, as per the course. This contains the password from the .env file again, so if you want to change the password for your MS SQL Server, you will need to change this in both places. This is also the case for the port, if you want to change that. Ideally, this would be generated based on the environment variables, but as this is a training course this is not necessary at this point.
  • These projects are set up to have the nullable enabled, and that means that by default any classes are not nullable unless otherwise specified. this was not fully supported in EF Core 5, but it is in EF Core 6, so when it comes to added dependent tables in a one-to-one setup, be sure to add the property as a nullable instead the default.
  • I've moved this over to use Postgres as of 2022-04-13, the original MS SQL Server container is still in the docker-compose.yml, however this is now commented out and the migrations were recreated to work with this
    • If you want to switch database server, it is best to remove all of the existing migrations and snapshot and then run dotnet ef migrations add init to create a new init migration for the new database server

pluralsight-efcore-getting-started's People

Contributors

cwrenhold avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.