user7700551
user7700551

Reputation:

Auto create tables (Code first) using Entity Framework WITH MYSQL

I'm writing this post because I need help with my ASP.NET Core MVC application. I'm using Entity Framework with Identity

This is the dbcontext class. The Settings DbSet is only for testing. Here Identity must create automatically all the tables such as AspNetUsers, AspNetRoles, etc as it does with a Sql Server, that is called

Code First if I am not wrong.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<SettingsDataModel> Settings { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

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

            // Fluent API
            modelBuilder.Entity<SettingsDataModel>().HasIndex(a => a.Name);
        }
    }

So, there I create the database class, the ASP.NET app inits the database here:

        services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySQL(IoCContainer.Configuration.GetConnectionString("DefaultConnection")));


        services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

In the Startup class. Note I'm using MySql because I will host the application in an App Engine on Google Cloud Platform, that is why the code has .UseMySQL. And in the HomeController is where I execute EnsureCreated method.

    protected ApplicationDbContext mContext;

    protected UserManager<ApplicationUser> mUserManager;

    protected SignInManager<ApplicationUser> mSignInManager;

    public HomeController(
        ApplicationDbContext context,
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager)
    {
        mContext = context;
        mUserManager = userManager;
        mSignInManager = signInManager;
    }

    public IActionResult Index()
    {
        mContext.Database.EnsureCreated(); //<-- It returns false

        if (!mContext.Settings.Any()) //<-- Here it throws an exception saying that the database no exists
        {
            mContext.Settings.Add(new SettingsDataModel
            {
                Name = "BackgroundColor",
                Value = "Red"
            });

            mContext.SaveChanges();
        }

        return View();
    }

I hope I explained correctly and I provided all needed classes to solve the problem.

Upvotes: 2

Views: 4446

Answers (1)

ElasticCode
ElasticCode

Reputation: 7867

In your ApplicationDbContext class make sure you add all needed class as public property like Settings Table.

public DbSet<PeopleDataModel> People { get; set; }

After that, you need to run Add-Migration AddPeople command that will create a new migration class with new changes, then run Update-Database command to apply pending migrations to the database.

Upvotes: 2

Related Questions