Charlie Salts
Charlie Salts

Reputation: 13488

Making changes to existing entities in Entity Framework

Suppose I have an existing database set up using Entity Framework. Is there a mechanism through which I can safely add or remove entities (or their properties) such that the database is altered automatically?

I know there's an option to "Update Model From Database". Is there an equivalent "Update Database From Model" ? Is there a way to configure Visual Studio to do this automatically?

Upvotes: 0

Views: 202

Answers (2)

Wouter de Kort
Wouter de Kort

Reputation: 39898

Entity Framework 4.3 has Code First Migration support.

EF helps you with checking the differences between your code and database and then generates code for you that handles this changes. You can use the NuGet package manager console to enable migrations, add a new migration and run them against your database (or create a sql script).

This blog explains how the Migrations work and this blog shows how you can use it with an existing database

Upvotes: 2

Asti
Asti

Reputation: 12667

Altering the database schema isn't a straightforward operation (has a column been renamed, or is it new column? Can the old type be converted to the new type?) that you can easily infer from the model.

EF doesn't alter the tables for you - it can Drop-Create the DB for you when you change it. However, if you change the existing database by hand to suit the model, EF doesn't seem to mind. It looks like what they check for is Hash(Model) = Hash(Tables).

Upvotes: 0

Related Questions