Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sample hello world project showing how to use code-first EF Core with MySql

NotificationsYou must be signed in to change notification settings

yoDon/mysql-enterprise-foundation-core

Repository files navigation

A small Hello World sample project showing how to use MySQL with EF Core in a code-first approach.

The MySql database and tables will be automatically created tomatch the C# classes in the project (that's referrect to as "code first" EF). For reference, I've checked ina sampleMigrations folder that contains automatically generatedcode to create those tables, but you should use the commands belowto trivially generate your own custom database creation code.

Also, as a general note, if you are actively reading about MySQL and EF Core, you should note there are two MySQL to EFadapters, one from Oracle and one from the Pomelo Foundation. The syntax for the two is nearly the same but theycapitalize theUseMySql() method differently -UseMySQL() vsUseMySql(). That tripped me up more than I'd like to admitwhen I was first trying to stitch together examples from different sourcesto get this working.

Setup

First, edit the connection string inappsettings.json to have the desireddatabase name, username, and password.

Then, to create the database and tables, run the following commands before running this project.

This approach of creating the database and tables from C# classes is called "code-first". Thedotnet ef commands will create a folder calledMigrations, populate it with C# code thatwill create the database and tables that match the project, and then then run themigrations to create the database and tables, all under the directionof your connection string inappsettings.json. For reference, I've checked ina sample Migrations folder, but you should use the commands belowto create your own.

dotnet tool install --global dotnet-efdotnet restorerm -r ./Migrationsdotnet ef migrations add Initial -o Migrations -c SampleDbContextdotnet ef database update -c SampleDbContext

Thanks

These articles helped me get this working:

About

Sample hello world project showing how to use code-first EF Core with MySql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp