PiousVenom
PiousVenom

Reputation: 6918

How to add a new table using Code First and migrations

I'm currently working on a project that is utilizing EF Code-First. Now, we've already made 3 tables in our database, and using migrations are able to update them just fine. Now, we've decided that we actually need another table in our database. I've created the class and the context for it. I assumed I could use a migration for this to update the database; however, when I created the migration, the file was completely empty. This particular table is not associated with any prexisting tables. How can I go about adding new tables like this?

Upvotes: 3

Views: 9062

Answers (2)

curiousdork
curiousdork

Reputation: 1034

You need to create a property in your DBContext and add mappings.

public DBSet<MyTable> MyTables {get;set;}

And you should have a mapping:

modelBuilder.Entity<MyTable>.HasKey(mt => mt.Id);
//...everything else...

When you run add-migration it should generate code for you/

Upvotes: 3

Parimal Raj
Parimal Raj

Reputation: 20595

Please follow this tuotrial : msdn.microsoft.com/en-us/data/jj591621

it shows all the steps need to perform migration on the code-first database

Upvotes: 0

Related Questions