1

I'm doing code first approach for the first time, and I created two classes (tables), and a context class.

Context

namespace Idle.Models.Database
{
    public class DatabaseContext : DbContext
    {
        public DatabaseContext() : base("name=Database") 
        {
        }

        public DbSet<Company> Companies{ get; set; }
        public DbSet<Car> Cars{ get; set; }
    }
}

And then I built the project.

But the tables weren't created in my database.

How do I then create my two tables?

1 Answer 1

1

1) Magic creation, just create your DbContext:

Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext >());

using (var databaseContext = new DatabaseContext ())
{
   // Do any stuff here
   databaseContext .SaveChanges();
   Console.WriteLine("Done");
}

2) If you want to use DbMigration: In Nuget console you have to use the command Add-Migration/Update-Migration More info:

https://msdn.microsoft.com/en-us/data/jj591621.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.