2

My problem lies in the lack of experience in MVC. Basically, I have two tables in DB -Person -Offer

For each I have created a model and a controller and a model, so the structure looks like that:

public class Offer
{
    public int OfferID { get; set; }
    public string OfferTitle { get; set; }
    public string State { get; set; }
    public string Description { get; set; }
}

public class OfferDBContext : DbContext
{
    public DbSet<Offer> Offers { get; set; }
}

This is the Offer model.

public class Person
{
    public int PersonID { get; set; }
    public string Username { get; set; }
    public DateTime Birthday { get; set; }
    public string Education { get; set; }
    public string Email { get; set; }
}

public class PersonDBContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
}

This is the Person model.

Firstly I created the Person model, that added itself to db without any problems. Then I wanted to add Offer table, and I had to use the DropCreateDatabaseIfModelChanges method. I used it for OfferInitializer and PersonInitializer and then there is the Global.asax.cs file

protected void Application_Start()
{
    Database.SetInitializer<OfferDBContext>(new OfferInitializer());
    Database.SetInitializer<PersonDBContext>(new PersonInitializer());

    //Database.SetInitializer<PersonDBContext>(new PersonInitializer());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

From what I understand, I cant do that simply because I am dropping database 2 times, each time populating only one table at a time. How do I reorganize it all, so that I can populate both or more tables at a time, or the whole database?

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
bazeusz
  • 75
  • 1
  • 6

1 Answers1

2

First things first, you should not create individual DbContext classes for each table. You should instead put all your DbSets in the same DbContext. Doing this will simplify things greatly for you.

Secondly, you should look into using migrations. You should start using them very early in your project.

You work with code first migrations using the Package Management Console.

enable-migrations

Does exactly what the name implies. Initializes migrations in your project. This will create a folder inside your project and generate the files needed.

add-migration InitialCreate

This creates a migration. InitialCreate is actually a string and you can change it to whatever you want. This command will generate the scripts needed to create the database from strach.

update-database

This command verifies the database and applies the migration (or migrations - there can be multiple) required in order to get the database up-to-date.

This is the initial setup. If you do further changes to your first code first classes, or add more, you will just have to add a new migration and then execute it.

add-migration AddedFirstName
update-database

It's that simple!

There are some more advanced concepts like seed, rollback, update to specific migration, etc., but what I have typed above covers the basics and the day to day usage of migrations.

I recommend you to read this article which explains everything in much more detail: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70