Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Changing Entity Framework consumed classes after the DB has been created and deployed

Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework Code First approach to working with database.

The system is now working in test environment.

If the part of the DbContext is the following:

public DbSet<Incident> Incidents { get; set; }

will it cause DB-Code collaboration problems, if I, for example, add Properties to an Incident class?

Or will the framework just update the Database tables automatically, and will treat the older records, that were created without new properties as if they are null?

Upvotes: 0

Views: 214

Answers (2)

Min Min
Min Min

Reputation: 6218

It depand on your database initialization setting. (CreateDatabaseIfNotExists, DropCreateDatabaseWhenModelChanges, DropCreateDatabaseAlways)

eg: Database.SetInitializer(new CreateDatabaseIfNotExists())

  • If you didn't set any database initialization, EF will use default initlizer "CreateDatabaseIfNotExists" and it would throw InvalidOperationError.

  • If you set as "DropCreateDatabaseWhenModelChanges", EF will recreate the Database based on your new Model. But you will also need necessary access right to create the database and it is dangerous if you are in production environment.

  • If you set as "DropCreateDatabaseAlways", EF will recreate the Database whenever you run the application.

Upvotes: 2

SidAhmed
SidAhmed

Reputation: 2360

As far as I know, it will not update the database automatically, you will have an error message telling you that the Context has been changed. There for, you have to do a "Drop and Create" to the database !!

Upvotes: 0

Related Questions