I'm using VB.net, EF6 with SQL Server 2008R2 database.
This is the situation: I have created the application. Using wizard I have created the Entity model from an existing database.
A client started using this application using this database on his computer.
After some months, I made some modifications on the database and I have updated the model on my application.
Now I have a new .exe file that has the new model from the new database.
I put the new .exe file on the client computer.
Now on his computer, the .exe file has the new database model, but the SQL Server database has the old structure.
I want to know: is possible to update the database structure from the entity model on application?
I want to add a command on application that can make (if is possible) this update, so the database become up to date according to entity model?
Thank you !
If you have applied code-first approach, then you should go for below solution , given on MSDN, so your generated script will be 'idempotent'.
Run the Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract command in Package Manager Console
after going through it and still facing issue then try below command
Update-Database -Script -SourceMigration:0
If possible, try and deploy the database as a data-tier application. It will make managing migrations and upgrades a lot easier. More info here, https://msdn.microsoft.com/en-us/library/ee210546.aspx
Related
I have an ASP.NET Web API project using .NET 6, SQL Server, EF6 and code first approach. I have already published the project (deckerized) on a server. A client now asked me to add a new column to one of my tables, if I do this by the EF6 migration, it is easy, but this ended up deleting my current table (dropping) and then made a new migration and table which I can not because there is data in the database now. What would be my approach to modify the database now after I published the project on a server. Do I need to use the TSQL in the SSMS environment for any modification to made a copy of my database to another place and do all the changes and then copy the table again to the new table?
Many thanks
I have a Visual Studio solution that has a few projects in it.
One project is based around an Entity Framework model .edmx.
Another project is an SQL Database project.
I can currently 'Update Model from Database...' in the .edmx.
I can also use 'Schema Compare...' in the SQL Database project to update the database.
I would rather not have to push the changes from the SQL Database project to the database, then update the .edmx from the database.
I would like to update the .edmx from the SQL Database project directly. Is this possible?
NOTE: this may not be a great solution having both projects, but it is a legacy product that uses Entity Framework. At one time, the project was Database First, and the .edmx would follow the changes. However, we want to track our SQL schema changes with the SQL Database project. Both projects work well to solve their individual problems, but don't quite work together?
I have Continuous delivery from TFS running to Azure for the C# project and this is fine.
I now want to the Continuous delivery to work with my SQL database.
Currently I have a SQL 2008 R2 database which holds the database.
What is the best option to ensure I can deliver using Continuous delivery from TFS to include the database changes?
The important factor is it needs to be automated upon checkin to TFS.
I've just run into this problem and found guidance hard to come by but have managed it in my application, I'm very new to MVC and Azure so excuse me if this isn't too detailed.
Here's what I did;
In my Global.asax.cs Application_start() I added:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
Where ApplicationDbContext is the name of your context. Remember to add references to your Models class and Migrations classes.
When you do this, I think migrations are run on your database when the context is first called and it does not drop and recreate your database.
The one problem I had was that I was getting an error saying that objects already existed in the database because none of the migration data in the __MigrarionHistory table was in there so it tried to create the database from scratch. Luckily my application wasn't live yet so I could delete the tables and commit to TFS and it recreated everything and included all of the __MigrationHistory so now, any new code first migrations are run on the first hit and the database is updated. I wouldn't know what to do if this database was live!
Hope that helps
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.
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.