Wednesday, July 6, 2011

Updated MySQL Connector/Net

Current MySQL Connector/Net is 6.3.7, and this version can do code first in limited condition. This post expresses the instruction of code first with MariaDB.

  1. Do until phosphorescence: Create Simple MVC 3 App with VWDExpress and MariaDB without table creation
  2. Create the initializer class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;

    namespace Mvc3Training.Models
    {
        public class Mvc3TrainingInitializer : DropCreateDatabaseAlways<Mvc3TrainingContext>
        {
        }
    }
    DropCreateDatabaseAlways is the one of superclasses we can select. Others are DropCreateDatabaseIfModelChanges and CreateDatabaseIfNotExists.
  3. Add mantra to Mvc3TrainingContext
    using System.Data.Entity.ModelConfiguration.Conventions;

    namespace Mvc3Training.Models
    {
        public class Mvc3TrainingContext : DbContext
        {
    ........
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            }
    ........
        }
    }
    So, that's the limit. We must abandon the convention for pluralizing table names. MySQL Connector/Net still has such a bug. If not, MySQL Connector/Net creates the table with singularized name and looks up the table with pluralized name.
  4. Add this code to Application_Start method in Global.aspx.cs
    Database.SetInitializer<Mvc3TrainingContext>(new Mvc3TrainingInitializer());
  5. Launch the server, than access to the controller you maked.

No comments:

Post a Comment