Create database from edmx entity 3.5 file programmatically - database

I'm wanting to create a database programmatically from the edmx or edmx.sqlce file in my project. I read the best practice when sending the data out to users this is the best way. But I haven't found anything on it. Or is it best to create it and send it out with the program? How would I do updates to the database if I did this? How do I tell which version the database is?
Sorry for the multiple questions in one. I'm new to programming/databases so I don't know how it is normally done and can't seem to find a book on it either.

The best practice in this scenario is creating installation package (.msi) which will install your application and execute script to deploy the database. The script can even check dependencies like SQL Server CE and .NET Framework. This will also solve your problem with database version because .msi installation package keeps this information so you can create upgrade .msi which will know that it must execute only change script for database instead of creating a new one. Be aware that creating installation packages is pretty complex task but this is the way how it is done in real products shipped to customers. In both my current and previous company we used WiX to create installation packages but we have special guy who does this.
There is no API to create database from EDMX file. Moreover EDMX file is not part of built application. Database generation is feature of VS2010 which uses either workflow or T4 template to transform EDMX into SQL script. The default template can create only full database script and you must execute it.
I described the way of creating deployment script for new version of database in separate question. It is related to code-first (EFv4.1) but the principle is the same. If you need to keep information about database version you can have special table for that and check the value in the table at the beginning of the upgrade script.

Related

Creating a generic update/install script from a Sql Server Database Project

Can you create a generalized deployment script from a Sql Server Db Project in VS 2015 that doesn't require a schema compare / publish against a specific target database?
Some background:
We are using Sql Server Database projects to manage our database schema. Primarily we are using the projects to generate dacpacs that get pushed out to our development environments. They also get used for brand new installations of our product. Recently we have developed an add-on to our product and have created a new db project for it, referencing our core project. For new installations of our product where clients want the add-on, our new project will be deployed.
The problem we are having is that we need to be able to generate a "generic" upgrade script. Most of our existing installations were not generated via these projects and all contain many "custom" stored procedures/etc specific to that client's installation. I am looking for a way to generate a script that would do an "If Not Exists/Create + Alter" without needing to specify the target database.
Our add-on project only contains stored procedures and a couple tables, all of which will be new to any client opting for this add-on. I need to avoid dropping items not in the project while being able to deploy all of our new "stuff". I've found the option to Include Composite Objects which I can uncheck so that the deployment is specific to our add-on, but publishing still requires me to specify a target database so that a schema compare can be performed and I get scripts that are specific to that particular database. I've played with pretty much every option and cannot find a solution.
Bottom Line: Is there a way for me to generate a generic script that I can give to my deployment team whenever the add-on is requested on an existing install without needing to do a schema compare or publish for each database directly from the project?
Right now I am maintaining a separate set of .sql files in our (non db) project following the if not exists/create+alter paradigm that match the items in the db project. These get concatenated during build of our add on so that we can give our deployment team a script to run. This is proving to be cumbersome and we'd like to be able to make use of the database projects for this, if at all possible.
Best solution is to give the dacpacs to your installers. They run SQLPackage (maybe through a batch file or PowerShell) to point it at the server/DB to update. It would then generate the script or update directly. Sounds like they already have access to the servers so should be able to do this. SQLPackage should also be included on the servers or it can be run locally for the installer as long as they can see the target DB. This might help: schottsql.wordpress.com/2012/11/08/ssdt-publishing-your-project
There are a couple of examples of using PowerShell to do this, but it depends on how much you need to control DB names or Server names. A simple batch file where you edit/replace the Server/DB Names might suffice. I definitely recommend a publish profile and if this is hitting customer databases they could have modified, setting the "do not drop if not in project" options that show up is almost essential. As long as your customers haven't made wholesale changes to core objects, you should be good to go.

ASP.NET MVC Code-First EF - Possible to use EF without database create permissions?

So I'm working on an ASP.NET project for university. We have to upload our code to a server running IIS and SQL Server 2008. I've written my project using MVC Code-First EF. I understand that the Entity Framework system needs permission to create the database to work properly (you can't just give it an empty database and let it fill it with data). This has created a problem for me since I do not have database creation privileges on the shared SQL Server. Is there any way around this?
As you don't have permissions, it sounds like you'd need to get a DBA to create your database on the server you are trying to deploy to - this could be done from either a database creation script or from a database backup of the db on your dev machine. You can then instruct EF code first not to try to create / update the database automatically by adding this line to your global.asax (or indeed anywhere before you first access the database)
Database.SetInitializer<YourContextType>(null);
You can use an existing database, rather than let EF create one for you. I have done this myself, but admittedly only when using EF Migrations. Otherwise, you run into trouble with missing table exceptions and what not.
When using migrations, just point your connection string to your empty database, create an initial migration to populate the database with your schema and then update the database itself.
See this answer: How do I create a migration for an existing database in EntityFramework 4.3?
.. which include this nice link to getting started with EF Migrations: http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/
All this is available through Nuget, and if you have access to Pluralsight content, I can highly recommend Julie Lerman's video on the topic.
If you don't want to use Migrations, you can still use Code First if you just create the database objects manually using SMMS, but obviously you have the manual work of keeping your model and the database in sync.

How to update database by ant (versioning)?

I have a web application on java, which is working with database. I need an ant’s script that will deploy or update my application to latest version. There is no problem with application part, but I don't know how to do database update.
I have an idea to build-in some meta-information (number of version) to the names of sql scripts.
For example:
DB_1.0.0.sql
DB_1.0.1.sql
DB_1.2.0.sql
DB_2.0.0.sql
DB_2.1.0.sql
My script detected, that current version was 1.0.1, so I need to execute DB_1.2.0.sql, DB_2.0.0.sql, DB_2.1.0.sql files by SQL task. Problem is: how to find files with ant, that I need to execute.
Maybe it is not the best way to update database. Do you have any other idea?
Flyway works as you've described. It keeps a record of the SQL files already applied to the database, enabling an automatic upgrade. Simple and straight forward to use.
A more powerful solution, IMHO, is liquibase. It has an XML syntax to record database changes, enabling the generation of cross-platform SQL. It also has some powerful features such as the ability to roll-back changes and perform diff's between databases.
It looks like your filenames follow a strict convetion. In that case you can find files by matching a pattern filelist and execute using sql.
You can use LiquiBase to write some tasks that can help in database schema updates-

How can I set my deployment options to script the incremental release of a Visual Studio 2010 database project?

I've just started using a VS2010 database project to manage the release of an update to an existing database.
I want the deployment option to generate a script that will contain the commands to change my existing database rather than create an entirely new one.
E.g I have 10 existing tables - one of which I drop in the new version and I create some new sprocs. I only want the deploy to script the Drop table and Create Procedure commands.
I am using VS2010 Premium.
Is there a recommended standard approach I could follow to managing DBs in a project from initial creation to incremental releases?
Thanks!
There is an "Always re-create database" in the project's .sqldeployment file. Unchecking this option will result in an auto-generated SQL script that will incrementally update your database without dropping it first.
There is also an option to "Generate DROP statements for objects that are in the target databse but that are not in the database project." You will need to check this option, if you want tables, stored procs, etc. to get dropped if you've deleted them in the database project. This will delete any table, etc. that users may have created on their own for testing, debugging, etc.
To change the options in the .sqldeployment file. Open the file in Visual Studio. Either expand the database project in the solution explorer, the double click on the .sqldeployment file (it will probably be in the Properties folder under the DB project). Or open the properties page for the database project and click the "Edit..." button next to the "Deployment configuration file". Check or uncheck the options you want when the database deploys.
I use VSDBCMD.exe for 1-click build & deploy scripts I've created. It works very well. VSDBCMD uses a .sqldeployment file -- the default .sqldeployment file is specified in the .deploymanifest file, but it can be overridden by specifying it as a parameter when executing VSDBCMD. Also, I believe that Visual Studio uses VSDBCMD under the covers when
it deploys the database project, but I just assume that to be the case since the functionality is pretty much identical.
I asked a similar question a while back on the MSDN Forums and was told that the recommended way to do this is to use VSDBCMD. Basically, you output a schema file from your database project which contains all of the information about your database, and then you run VSDBCMD to compare your schema to the target database. This in turn creates the script to update the target database to your current schema.
The rationale for this approach is that just because you and I may think we know what the target database's schema looks like we can't really be sure until we let VSDBCMD run the comparison. Who knows, someone else may have modified the schema in the target database without our knowledge, so our change script may end up failing for some unknown reason.
I really wasn't terribly satisfied with this approach and ended up continuing to use my "old approach" of hand-coding my change scripts when necessary, but I am eager to see if anything has changed in 2010 that makes this a bit easier to work with. I'd really like to see a simple API that does what VSDBCMD does so I can put a GUI together to simplify updating a target (in my case, client) database without the person running the upgrade having to be a DBA.

What is the best way to deploy SQL Server scripts in source control?

I am trying to get our company's database objects stored in source control. We are going to be stuck using Aldon LM for our source control program. What is the best way to deploy scripts that we create to our SQL Server environments automatically from source control?
For our .Net web applications, we have configured Aldon to push our code to the web server and run MSBuild.
We have programs (ant hill pro for one) which go through the source control system, and retrieve all the necessary sql scripts and compile them into one file. We have a branch for each release, so only the ones for that branch get get pulled. I think cruise control can do the same thing, but I don't know how that is done.
We then can execute all the updates and once and don't have to worry about missing one.
Open a Database project in your solution to manage all SQL code such as stored procedures. This has a few advantages:
Your SQL can be under version control just as the rest of your conde
You can do easy find/replace file operations on multiple procedures
You can keep your SQL in a meaningful and systematic folder hierarchy
Install VS Team System Database Edition, now included (available to add to) Developer Edition.
This has tools to work out which scripts to apply to a database to update it to match the reference database and will (providing your SCM integrates with VS) also handle tracking all those scripts and there status within the project.

Resources