heymega
heymega

Reputation: 9391

Entity Framework Code First Don't Create Table

I'm using an existing database and I have mapped one of the tables as an entity (as i needed to map a foreign key).

So when it comes to initialising this database I would like EF to ignore this entity since it already exists.

How would I go about doing this?

Upvotes: 3

Views: 4204

Answers (2)

Steven V
Steven V

Reputation: 16595

You should create an empty migration as the first migration with the -IgnoreChanges flag to allow Entity Framework to get a snapshot of the existing database.

So out of the gate use:

Add-Migration InitialMigration -IgnoreChanges

and that will create a blank migration but it will update the Entity Framework metadata allowing the existing tables to exist and not be touched by migrations.

Also to be mentioned that the naming conventions that Entity Framework expects and your database schema may differ. You may need to manually setup the foreign keys using the Fluent API.

Upvotes: 4

rouen
rouen

Reputation: 5124

I didnt check with EF 6 specifically, but I think default EF behavious is that when the database exists, then it presume all model be ready and therefore will create no tables. If you want your initialization code to create tables with code first, use initialization code for prepare data. Look here :

http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx

Upvotes: 0

Related Questions