Entity Framework code first - isn't it meant to create tables?

c# dnx entity-framework entity-framework-6

Question

After MUCH research into Entity Framework (targeting DNX 4.5.1) I keep coming to the same conclusion that when using the code-first approach EF is supposed to create any tables that do not exist.

DbContext:

public class AccountReadContext : DbContext
    {
        public AccountReadContext(DbContextOptions options)
        : base(options)
        {}

        public DbSet<AccountRead> AccountReads { get; set; }  
    }

Database Provider:

public void AddAccountRead(AccountRead reads)
        {
            _dbContext.AccountReads.Add(reads);
            _dbContext.SaveChanges(); //System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'AccountRead'
        }

I know why this is happening (the table is missing). What I can't figure out is how do I get code-first to do its thing and create the table?

1
2
5/18/2016 10:49:50 AM

Accepted Answer

In the end I had to use the EntityFramework commands in the project's root folder:

Update database:
dnx ef database update -c "AccountReadContext"

Add / Remove columns (specify column name):
dnx ef migrations add Reading -c "AccountReadContext"
dnx ef database update -c "AccountReadContext"

This provides a bit more info

0
5/19/2016 8:06:16 AM

Popular Answer

Automatic Migrations should be enabled by default. If you disabled them (AutomaticMigrations = false), you should run Update-Database command manually, or set the Database Initializer to MigrateDatabaseToLatestVersion<,>



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow