I am using EF Code First in an MVC project. I am wondering if it is possible to configure it so that it will create the database and seed the tables only if the database does not exisit.
Right now I have it with DropCreateDatabaseIfModelChanges but I don't want to drop or seed the database ever, unless it doesn't exist.
From what I read that is the default that EF Code First uses. I guess my question is now, is it safe to set my initializer to this and go to production without any other changes?
I also found that it's possible to set the initializer in the web.config instead of the global.asax and when going to production I can just remove the initialization app config setting so that EF Code First doesn't do the initialization.
You don't have to change the code when you go to production, you can just change a Web.config setting -- see Preventing Entity Framework Code First from Dropping the Production Database in this tutorial:
beetledev
Member
750 Points
173 Posts
Configure Code First to create and seed database only if it does not exist
Apr 09, 2012 03:37 AM|LINK
I am using EF Code First in an MVC project. I am wondering if it is possible to configure it so that it will create the database and seed the tables only if the database does not exisit.
Right now I have it with DropCreateDatabaseIfModelChanges but I don't want to drop or seed the database ever, unless it doesn't exist.
beetledev
Member
750 Points
173 Posts
Re: Configure Code First to create and seed database only if it does not exist
Apr 09, 2012 04:21 AM|LINK
I found this: CreateDatabaseIfNotExists
From what I read that is the default that EF Code First uses. I guess my question is now, is it safe to set my initializer to this and go to production without any other changes?
I also found that it's possible to set the initializer in the web.config instead of the global.asax and when going to production I can just remove the initialization app config setting so that EF Code First doesn't do the initialization.
Is there anything else to be aware of?
jsiahaan
Contributor
2304 Points
588 Posts
Re: Configure Code First to create and seed database only if it does not exist
Apr 09, 2012 04:49 AM|LINK
Nope, remove the initializer if you are going to production.
Indonesian Humanitarian Foundation
tdykstra
Contributor
4446 Points
622 Posts
Microsoft
Moderator
Re: Configure Code First to create and seed database only if it does not exist
Apr 09, 2012 02:37 PM|LINK
You don't have to change the code when you go to production, you can just change a Web.config setting -- see Preventing Entity Framework Code First from Dropping the Production Database in this tutorial:
http://www.asp.net/web-forms/tutorials/deployment-to-a-hosting-provider/deployment-to-a-hosting-provider-web-config-file-transformations-3-of-12
You should also consider using Migrations in the latest release of Entity Framework (4.3.1), which will facilitate database changes after you're in production. See http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx
beetledev
Member
750 Points
173 Posts
Re: Configure Code First to create and seed database only if it does not exist
Apr 09, 2012 04:35 PM|LINK
Thanks both, for the replies and thank you tdykstra for the additional links.