Thursday, July 29, 2021
In this post we are going to create our first Entity Framework migration and creating an actual database in MySQL.
In order to do that we need to add a few NuGet packages to our Asp.Net Core project including the Entity Framework Core package. But, before we do that we want to find out what what version of .NET Core we are running. Obviously I have it memorized, but for the rest of you, you can type the command dotnet --version to figure out what the version is :) It's always a good idea to try and match the package version with the .NET Core runtime you have. It's one of the more annoying thing with Angular and Asp.Net Core, it's changing constantly. So as you can see my version 3.1.xxx so I should try to install packages that are made for 3.1.x.
The first package we are going to install is the Microsoft.EntityFrameworkCore. So open the ACMEBank.API project with Visual Studio Code. Press Ctrl+Shift+P and type NuGet and select
"NuGet Package Manager: Add Package", then type Micrsoft.EntityFrameworkCore press Enter, select the package like in the screenshot
You want to select the version that matches the .NET Core runtime, once you select the version, a "restore" button will appear at the bottom of the screen, click on it to add it to the project.
Now, the do the same thing with the following packages:
- Pomelo.EntityFrameworkCore.MySql
- Microsoft.EntityFrameworkCore.Design
Open the appSettings.json file and add the following lines after the first { character
"ConnectionStrings": { "DefaultConnection":"Server=localhost; Database=acmebank; Uid=devuser; Pwd=P@ssw0rd" },
The completed file should look like the following
{ "ConnectionStrings": { "DefaultConnection":"Server=localhost; Database=acmebank; Uid=devuser; Pwd=P@ssw0rd" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" }
Now we are ready to write some code to create our database using the code first approach. First create a folder at the root of the project call Models, and create a class call Value. This class will be used by Entity Framework our columns for the Values table that we will create.
namespace ACMEBank.API.Models { public class Value { public int Id { get; set; } public string Name { get; set; } } }
Now create a folder in the root of the project call "Entities" and create a class call EntitiesContext
using ACMEBank.API.Models; using Microsoft.EntityFrameworkCore; namespace ACMEBank.API.Entities { public class EntitiesContext : DbContext { public EntitiesContext(DbContextOptionsoptions) : base(options){} public DbSet Values {get;set;} } }
Finally, we are ready to make our last code change, open the Startup.cs file and add the following line to the ConfigureServices method
services.AddDbContext(x => x.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
The completed code should look like the following
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(x => x.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); services.AddControllers(); }")));
Your blog is very informative, finally, I found exactly what I want. Paypal is an excellent service for online payments but lots of its users confront issues while they access Paypal. If you want to resolve your problems then must visit contact Paypal Nederland.
ReplyDeleteYour blog is very informative and interesting to read, finally, I found exactly what I search for. There are lots of users of Macfee antivirus in the world because of its features and easy interface. If you want to explore more interesting facts about Mcafee antivirus or want to resolve your technical issues then must visit Mcafee ondersteuning nummer.
ReplyDelete
ReplyDeleteWOW!...I'm having trouble to arrange my words, I'm very enthusiastic to see your post
Love back Solution Specialist
Love Marriage Specialist
love marriage specialist astrologer Love marriage specialist in Hyderabad love marriage specialist in Bangalore Love marriage specialist in Kolkata Love marriage astrology Love marriage specialist in Qatar Love marriage specialist in Italy