This continues from http://dev-blog.dusyk.ca/entity-framework-and-mysql/.

Creating the new entities

To do code-first migrations, we need to first create the entities. Start by creating the Order class. Right-click on the Models\Entities folder and to go Add > Class...:

Add class

Enter "Order.cs" for the Name and click "Add":

Add order

In the new Order.cs file, enter the following code:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MariaCodeFirst.Models.Entities
{
    [Table("Orders")]
    public class Order
    {
        public int OrderId { get; set; }

        [Required]
        public int CustomerId { get; set; }
        public virtual Customer Customer { get; set; }

        public DateTime CreatedDate { get; set; }
    }
}

So that it looks like:

Order class

Next, add the Product class in Product.cs:

Add product

Put the following code in the new file:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MariaCodeFirst.Models.Entities
{
    [Table("Products")]
    public class Product
    {
        public int ProductId { get; set; }

        [Required]
        [StringLength(5)]
        public string ProductCode { get; set; }

        [Required]
        [StringLength(150)]
        public string Description { get; set; }

        public DateTime CreatedDate { get; set; }

        public DateTime LastModifiedDate { get; set; }
    }
}

So that it looks like:

Product class

Finally, we need to add the OrderProduct class. Create OrderProduct.cs:

Add orderproduct

Put the following code in the file:

using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace MariaCodeFirst.Models.Entities
{
    [Table("OrderProducts")]
    public class OrderProduct
    {
        public int OrderProductId { get; set; }

        public int OrderId { get; set; }
        public virtual Order Order { get; set; }

        public int ProductId { get; set; }
        public virtual Product Product { get; set; }

        public DateTime CreatedDate { get; set; }
    }
}

So that it looks like:

OrderProduct class

Now that the entities are created, the DbSet for each class needs to be added to the Context class. Go to MariaDBContext.cs and add the following lines to the partial class:

public virtual DbSet<Customer> Orders { get; set; }
public virtual DbSet<Order> Products { get; set; }
public virtual DbSet<OrderProduct> OrderProducts { get; set; }

The class should now look like:

MariaDBContext

Creating and running the migration

Now that the entities have been created and added to the context, it's time to create the actual migration and update the database. This is actually a fairly simple process thankfully!

First Open the Package Manager Console in Visual Studio. Once it initializes, type the following in and hit Enter:

Add-Migration 'OrdersAndProducts'

This will create the migration, and you should a message underneath when it completes:

Add-Migration

This has also created a new migration file collection in the Migrations folder in your solution:

Migrations folder

Now that the migration is created, it's time to run the migration against the database. In the Package Manager Console, type in the following and hit Enter:

Update-Database -Verbose

This will apply the migration to the database specified in the Web.config file. The -Verbose flag forces verbose output from the migration, and typically you won't need it unless you want to see each step being executed. You should see the following output:

Update output

This means the migration has been applied successfully! If you connect to the database with a MySQL Client, you can see the tables listed with all of their FK relationships:

Database

Unfortunately, when I was setting up my local instance of MariaDB, I forgot to set the case conventions for database artifacts, so everything appears here in lower case. However, you can configure it to allow the MS standard of UpperCamelCase. The sample code for this can be found on GitHub.