Code first with mysql
Code first with mysql
I’m going to use ASP.NET API, build the data layer code-first and do
code-based registration.
Prerequisites
I’m asuming you know how to program, what an ORM does (why
would you otherwise want to use EF6?) and that you want to install
on a Windows machine. I’m using the following (NuGet) versions:
• .NET 6
• MySQL Workbench
EntityFramework
MySql.Data
MySql.Data.EntityFramework
The DbContext is the part that magically works with your database.
Trust the magic :)
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class SchoolContext : DbContext
{
To code how each field in a table will look (what the max. char size of
a string will be for instance) you can code in the override method of
OnModelCreating in the SchoolContext using FluidAPI or you use
the annotations in the models themselves.
ConnectionString
<configuration>
<connectionStrings>
<add name="SchoolContext"
providerName="MySql.Data.MySqlClient"
connectionString="server=localhost;port=3306;database=SchoolDatabase;uid=r
oot;password=YourSuperSafePassword" />
</connectionStrings>
</configuration>
Now comes the part where I forgot how it worked, and what I think
is not explained properly in the tutorial from Microsoft: create the
actual database from code.