Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 481
PiousVenom Avatar asked Jun 08 '26 17:06

PiousVenom


1 Answers

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/

like image 191
curiousdork Avatar answered Jun 10 '26 06:06

curiousdork



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!