I have an application that uses a database with one table currently. I want to add another column to the table. According to this post(http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part7-cs), there are 2 ways to modify the database: 1. Have EF drop and recreate the db or 2. Explicitly modify the db. Unfortunately, they go over the first method.
What is the correct / easiest way to do the second method if I have a table called Team?
The columns I have are TeamID, TeamAbbreviation, TeamCity, TeamName and TeamDisplayName. I want to add TeamSmallLogoUrl.
To do the second method, just go modify the DB and add your column. Then open the EDMX file in VS (double-click it in the Solution Explorer) and right-click on it when it opens up then choose "Update Model from Database" from the context menu. Click Finish. It'll pick up your added column.
Assuming you are using code-first (hence the EF-4.1 title):
If you are still in development phase and there are no important data in the database, use the Drop-Recreate method. (this is also the easiest way).
If you have some data in the database, just do this manually. The names of the properties and class should match/map directly unto your database schema. (if not mapped, EF will throw an error anyways)
Related
Okay, so this is my situation. I have generated POCOs from a db and developed around them. I have also renamed and changed some of these POCOs to be more readable. I have now made changes to my original db scheme and need to migrate the changes back to my models without overriding my changes except where the scheme has changed. It there a way to generate a *.sql script to hand to by DBAs? If so, it there a way to compare two dbs and generate a change script? ie - dev db => prod db.
You can switch a project to code first and generate the scripts you need via migrations. See the link below for a guide on moving from db first to code first, but it sounds like you may be partially there already.
1) enable-migrations for the project with your context if you haven't already.
2) create a baseline migration. EF will use this as a starting point so you won't get a bunch of code to create the objects that already exist. The ignore changes flag tells EF not to create the existing objects. https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396#option1
create-migration InitialCodeFirst -IgnoreChanges
3) Now modify your schema as you normally would and create a migration:
add-migration SomeNewThing
4) Create a script for a different database (like PROD) by using -Script. This will not update your database it just creates a script, so I usually run it a second time without -Script:
update-database -Script // creates a script (in VS), but does not apply
update-database // updates the database your connect string points to
5) DBA runs script and this will add a record to __MigrationHistory to identify it as being applied.
http://devgush.com/2014/02/24/migrating-a-project-from-database-first-to-code-first/
Here is a useful link on deployment: http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1
In Entity Framework, I always use the Update module from Database option, but It's not covering all kind of updates, sometimes it's more easier to delete the EF and add it back than fixing and verifying whether this option reflected the updates or not,
In my current project i have added a Self-Join relation and then i used the Update module from Database option, but it didn't update it correctly,
I can find that table and the new relation in the Module diagram but in the code behind it's not reflecting the update correctly, This time i have my custom changes on the EF? It is not possible to delete and add again this time, My question is:
Is there any reliable and easy way to update the EF from DB without
using third party tools?
After clicking the Update module from Database , Two clicks are needed, orderly:
Right Click on the .tt file and choose Run Custom Tool. Wait Until is Finished.
Right Click on the .Context.tt file and choose Run Custom Tool. Wait Until is Finished.
I used this method in Visual Studio 2012 for many scenarios and it's updating fine with no errors.
Hope this helps.
I've a ASP.NET MVC4 EF DatabaseFirst Application. It's currently up & running. Now, I will have to make some changes to the Database Schema and re-deploy again. So when I make the DB Schema changes, how do I update the Model, and also which all files should I be updating.
Can someone point me to a tutorial for this.
Thanks
If you're using an ADO.NET entity data model, open up your Model.edmx, right-click anywhere and select Update Model from Database in the context menu.
If you've added new tables to the database you'll need to mark the checkbox "Tables" under the Add tab. If you've only modified existing tables, you can just press Finish straight away.
Just wanted to know if there was an easy way to backup Stored Procedures and / or User Defined Functions ?
As a developer, one would usually want to retain existing versions of various database objects on production ( i.e. objects like tables / views / triggers / SPs / UDFs / anything MS manages ) so as to be able to revert to the most recent state of the database in case the situation for a rollback were to arise.
We know a backup of a db would fit the bill, but would be an overkill if the change was simply 1 SP.
At present, the process is manual and therefore time-consuming and also prone to human error, for a mundane task / should really have been automated.
Am asking if it were somehow possible ( or at least if it is in MS's dev pipeline ) to set a server state so it knows to "backup" anything that is altered. Therefore, every db object would then have an "archive" or "older versions" folder that one could use to browse the object's X most recent versions.
I don't know if you were aware of it, but you can use source control with SSMS.
Better yet, see Working with Database Projects. Database Projects in Visual Studio 2010 bring database developers many of the features that "code" developers have had since forever, including source control and automated deployment.
In Object explorer go to the desired Database. Right mouse click to open the menu. Select option Tasks>>generate scripts. The wizard will walk you through the process. It is sometimes tricky, so play around with it. Good luck.
I wrote a command-line utility called SMOscript which scripts all database object definitions to file.
One solution that I've seen employed is that you rename the existing object with sp_rename and then deploy your new one. so, if you have sp_foobar in your database and you want to deploy a new version of it, you'd rename the existing one to sp_foobar_20111017012345 and then deploy your new one. If you need to revert to the old version (or any previous version), you'd do a select name from sys.objects where name like 'sp_foobar%', find the right one, drop sp_foobar, and use sp_rename to rename the appropriate one back to sp_foobar.
I added am edmx file and a table to my project. How can I get it to generate CRUD operations? I did this in the past where it generated them as stored procs, but cant find the option on the table properties. What did I do wrong?
UPDATE
After fiddling about, I discovered I am confusing my tools. With a DataSet.xsd I can drag a table onto the Dataset Designer and click the *TableName*TableAdapter. If you view the properties window you will see "DeleteCommand, InsertCommand, SelectCommand, UpdateCommand". Is there a way to accomplish this task with Entity Framework too?
(If you have any trouble seeing this you can right click the table in dataset designer > Configure > Advanced options > check "Generate Insert, Update and Delete statements".
Since no one has responded with a method to accomplish the task, then I have to assume the functionality does not exist.