mysql database codefirst generation

Add Reference

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Tools
  • Pomelo.EntityFrameworkCore.MySql

Creating Entity Objects

Here the two entities to create objects, the way to demonstrate the effect of adding a foreign key 

public class TUser
{
    public int ID { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public ICollection<TRole> TRoles { get; set; }
}
public class TRole
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Generating a data table, the attribute "ID" became the default increment primary key.

It said two entities generated object data table, TRole TUser will contain the foreign key, the default is named TUserID.

Create a context object

public class MyDbContext:DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
         : base(options)
    {
    } 

    public DbSet<TUser> Users { get; set; }
    public DbSet<TRole> Roles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);     
    }
}           

 

The generated database file, the type of the entity wants to generate a data table in the form of the above code as the attribute name MyDbContext self-defined database context, named by the user. Other code can remain unchanged.

 

Add a database connection string

In appsettings.json added to the connection string (green under the bottom part):

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MyDbContext": "server=localhost;database=MyDb;user=myUsername;password=myPwd;"
  }
}

 

Replace your mysql localhost address, MyDb is going to generate a database name of the data table, myUsername for the mysql user name, myPwd for the mysql password.

Add contextual data services

Add the following code ConfigureServices class method StartUp

services.AddDbContext<MyDbContext>(options => 
    options.UseMySql(Configuration.GetConnectionString("MyDbContext")));

This code we wrote above, this class of MyDbContext registered as a service data context, follow-up calls can easily through DI. Configuration.GetConnectionString (string name) acquired in appsettings.json "ConnectionStrings" This part of the string corresponding to the name.

Generate the database

With vs2019, then direct the menu bar "Tools" -NuGet Package Manager - Package Manager console.

Enter the following two commands in the open window

  • Add-Migration InitialCreate
  • Update-Database

The first command generates a file, code written record of all our impact on the database, the generated files are automatically placed under Migrations folder, this folder is also automatically generated, the first command in the "Initial Create" used to name the database operation, named themselves.

The second command will operate on the database file migration according to the first command is generated.

carry out

In this case, mysql database should be able to see TUser and TRole the two data tables.

Guess you like

Origin www.cnblogs.com/zhaochenxi/p/11758141.html