Database Providers [4 of 5] | Entity Framework Core for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC]. >> Hello again, friends. Welcome back to Entity Framework Core for beginners. In previous videos, we've looked at using Entity Framework Core exclusively with SQL Server. In this video, we're going to look at using Entity Framework Core with other database providers. The database provider is a layer in the EF Core architecture that's responsible for the communication between Entity Framework Core and the database. It's a pluggable architecture, which means EF Core can support all databases. The first database provider we're going to look at is SQLite. SQLite is an open source cross-platform embedded database technology. In SQLite, the entire database is stored as a single file. It's a great choice when you don't want to take a dependency on a server-based database platform. I'm starting with a console application pre-configured with my new kit packages. I've already written context and model classes. The code in program.cs adds some records to the product's table. Using a different database provider with EF Core is often as simple as using a different NuGet package. To use the SQLite provider, I'm going to install the Microsoft.EntityFrameworkCore.SQLite package. I'll uninstall the SQL Server package too. Then I'll go to the on-configuring method in ContosoPizza context and configure the context with the UseSQLite extension method. The connection string points to the file to be created. Now, I'll create my initial create migration. After that, I'll use the update database command to run the migrations. The ContosoPizza DB file was created. Before we run, since I'm using Visual Studio, there's one other thing I need to check. By default, Visual Studio wants to start the app in a different directory than the build directory. If it does that, my app won't find the database. Let's add a start working directory element to the CS proj so that doesn't happen. Now let's run the app. It's done running. Let's see if we got any data in our file. In Visual Studio, I'll right click and get the path of the database file, which I'm going to use to open in an open source tool called DB browser. As you can see, the product data looks as we expected. Now, let's do that same exercise again, but this time, let's use Postgres. The database provider for Postgres is provided by the Postgres developer community. I'll add the Npgsql.EntityFrameworkCore.PostgreSQL package to the project and delete the SQLite package. Now, I'll replace the call to the .UseSqLite method with a call to use Npgsql. Let me paste in my connection string. Now, I'll generate and run new migrations. Let's run the app. The app started running. I'll switch over to the pqadmin tool and query the database. As you can see, the product table has data. The final database provider I'm going to show you is for Azure Cosmos DB. Cosmos DB is a fully managed no SQL database for modern app development. This means that instead of working like a relational database management system, it works with JSON documents. I've already created a free Cosmos DB account configured to use the core API. I haven't added any database containers yet. It's just an empty Cosmos account. I'll go to the Keys tab to get my connection string, then I'll go back to Visual Studio. I'll add the Microsoft.EntityFrameworkCore.Cosmos package to the project and remove the Postgres package. This time, I'll call the UseCosmos method. In addition to the connection string, I must also specify a database name. Since Cosmos DB is a no SQL database and there aren't any database schemas to update, let's delete the migrations folder. Since there are no migrations to create the initial database, let's call context.Database.EnsureCreated in program.cs to make sure the database gets created. Ensure Created can cause performance issues so avoid using it in production. One other difference we need to account for with Cosmos DB is the primary keys. The other databases we used support auto generating a primary identifier. Since Cosmos DB doesn't do that, we need to handle it ourselves. I'll change the ID in the product class to a string. I'll initialize it as a globally unique identifier. Since this primary key is a foreign key on order details, I'll change product ID in that class to be a string as well and I'll assign it to NULL along with the NULL forgiving operator. Let's run the app. Now that that's done, let's go over to the Azure Portal where we can see the data we just added. In this video, we looked at how EF Core uses database providers to support a plethora of different database technologies. We looked at just three of the possible database providers you can use. Visit the entity framework for documentation to see the full list of supported database providers. In the next video, I'm going to show you some tips to optimize performance in your EF core applications. [MUSIC]
Info
Channel: dotnet
Views: 26,361
Rating: undefined out of 5
Keywords: .NET
Id: moRmKo3nrN4
Channel Id: undefined
Length: 9min 8sec (548 seconds)
Published: Tue Apr 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.