1

I have an existing ASP.Net Web Forms application which is using EF Code First with Existing Database i.e. I am using EF classes and DbContext to point to an existing database.

I want to give the project the functionality to create/edit tables/fields and just wondering if people can recommend the best way to do this. I can't use migrations because the project is used on several different servers/databases.

I thought about putting something in the Global.asax file in Application_Start using SqlCommand. Is this a good idea or can you suggest a better way to do this, preferably with EF?

I'm not sure what if any code would be helpful so please let me know if there is anything you would like me to add to the question.

EDIT: Based on answers so far felt I should also note that I cannot directly access the servers the application is installed on because they belong to clients. Project is deployed locally, zipped and uploaded onto a site for their download.

1
  • Yes, but there is no problem with the use of EF Migrations and your EDIT :) EF Migrations do SQL Schema Update programmatically, at runtime Commented Jun 10, 2014 at 13:04

2 Answers 2

1

When using EF Code First, you should use EF Code First Migrations, although you say it's impossible.

Your only reason not to use this is the multiple database servers. Do you use Distributed Transactions? Otherwise, the only variable is the ConnectionString to the database server, and EF Migrations will do all the work for you to update your SQL schema.

Sign up to request clarification or add additional context in comments.

4 Comments

I apologise for my ignorance there, I should have said I believe I can't use them instead. I believe it's impossible because the servers are clients which I do not have direct access to and therefore cannot deploy or add migrations to directly. I had thought migrations had to be run within visual studio, can they be run in code and can I create them manually without using the package manager to create them?
No, Migrations are running when your application server is running. So you say the connection strings are on servers of your customers. that's fine, the SQL schema would be updated by EF Migrations when the customer will launch your application
So in theory if I: enable migrations for the project, add new class/model and add to dbcontext as dbset and add the migration. I should be able to simply deploy and when a client downloads and application is restarted the migration should take effect?
Yes, that is the same as if you uploaded your package to your azure distant website
0

It's probably a bad idea to do this as part of application startup - it'll require that the user that your application connects to the database with has escalated privileges in order to create/edit tables.

You can use migrations to initialise a database as part of a deployment process using the migrate.exe which is part of the EF NuGet package.

It's probably also useful to read a bit more around migrations - there is support for multiple contexts that can each be migrated separately...

Alternatively you could use a SQL script as part of your deployment process but then you'd need to manage the SQL by hand...

1 Comment

The issue I have is that the servers the application is on are clients that have purchased the application so I cannot deploy directly to their servers. The project is deployed locally, zipped and uploaded (without web.config to avoid overwriting sql connection string and other application values), and console application on server downloads and unzips accordingly. Will migrations have the ability to run in this way or can be called in code to accomplish this? I want to avoid scripts if possible and keep it in the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.