i'm new to MVC and have to update my model.
I have an SQL Server database already connected to my project, today I add a new column. Now I need to update my MVC code to reflect the change.
Everything online tells me to update the .edmx and .tt files with the Update Model Wizard since they are auto generated, but I cannot find the wizard in VS 2012 so I am at a stand still.
Is this the right way I should be doing this? Any tips would be great...thanks
There are a couple options you can do:
If the database is empty or there isn't a lot of data that you care about, you can drop the database and recreate it.
Manually add the column in the database (after property is in model)
There is something called Code First Migrations. This will allow you to update your database. Here is a tutorial you can look at for this: http://msdn.microsoft.com/en-us/data/jj591621.aspx
Double click the edmx file. This opens the diagram of the database model.
Right click and select "Update Model from Database..."
That will start the wizard you're looking for.
Be aware that not all changes come through automatically.
Most obvious is that columns will not be removed from model just because they're gone from the db. If you're dealing with lots of modifications or deletes rather than additions, it may be best to just drop the model and recreate it (as suggested above by Andrew).
No matter which approach you take, I'd suggest making sure you have all other local source changes committed or shelved before starting because you may need to do an undo check out if EF gets freaked out.
I ended up downloading this update of VS 2012 and repaired. Now I am able to update my model.
Related
I don't think I need to post any code but if I need to I will.
I have created a Windows Form in Visual Studio that creates a record and saves it into a database. I have also added an update function so that I can change the record and save the new changes.
What I need to know though is how to create some kind of version history. i.e. When I click update, I want it to save the previous version so that I can look back at it. Like a bug tracker has a kind of version control but a lot simpler (hopefully).
Would I need a separate database? Could I have another datagridview that when a record is clicked on (or a button) it will show the history of changes for that record?
I've done many searches online and just can't find how to do it or where to look exactly to find this?
Any help would be greatly appreciated.
Thanks
you are talking about audit trail , take look at this :Link
I know this example for MVC but it's the same idea
you only need one table to track changes of the whole database by using json objects that represent "before" and "after"
I have a question. I have a project that uses an edmx and all hooks up fine to the sql server.
I have installed it in several servers.
What I want to know is, having made changes in development so m database and my edmx are change,s say a new table and a modified one.
How do I make it so these updates are aplied automatically when I publish an app update?
I remember once a long time ago doing code first database work and making some kind of file or procedure that would automatically apply these changes when the app ran.
Can any one fill me in again.
1.- AFAIK, when you're using model first or database first, you can't use Migrations, you generate a DDL script that when executed, creates the database or drops and recreates tables losing any data you had.
See This link read point number 5.
"The script that is generated will drop all existing tables and then recreate the schema from scratch. This may work for local development but is not a viable for pushing changes to a database that has already been deployed. If you need to publish changes to a database that has already been deployed, you will need to edit the script or use a schema compare tool to calculate a migration script."
What I do here, is make the changes to the database and update the model (Right click edmx designer, click "update model from database")
2.- You can configure updates on a ClickOnce installer, any changes made to the edmx will be published on your app.
I have a local, SQL Server Compact database in an application I am creating. I generated a .dbml file I can use for LINQ-to-SQL purposes using the SqlMetal.exe tool, which worked fine - I now have the table objects for use in my application.
I have a strange issue though. Even after calling SubmitChanges() on my DataContext, the data never commits. But strangely, after adding the rows, these rows do appear in the table object on the datacontext, but not in the table. What shocked me is even after stopping and starting my application, these rows still existed in the DataContext - but after a few minutes they seem to disappear.
Have I configured this correctly? Are there any more steps I need to do after using SqlMetal to allow the LINQ to commit changes?
I am assuming that you linked to the dbml file using the default properties. This means that, each time you start a debug session, the file will be copied into your output directory and changes made to it will only be seen for that session (i.e., "Copy -> Always").
If you want the changes to persist then right click the file -> properties -> Copy Never. By default the IDE assumes that you don't want to modify the original database, only a copy for debugging purposes.
I'm newbie with .NET Entity Framework 4.0 I'm using VS2010 and I created the EDMX file and then added my Entities in this model. After I've finished my Datamodel and set the Asscociation, I right click on and choose Generate DB from model.
Then it creates SQL scripts *.sql then I open the SQL and right click on Execute SQL Statements... What happens is all my tables are re-created...
ISSUE: All my test data in MasterData Tables are removed/deleted. I need to add the data back again manually...
I googled on "keep data generated Data model EDMX" etc. but couldn't find... Can someone advice me how I can avoid this problem?
Because I've e..g 15 MasterDataTable + other tables linked with these and I need to re-add the records back manually for all these tables... and 9 of 10 case I don't touch those tables in UI.
please advice how I can avoid this situation.
thanks
You can update the model from the database (the reverse way you are taking now):
Double click the .edmx file to open the designer. In the middle of the designer, right click to show the contextual menu and click "update model from database". It will prompt a screen, check the DB parameters and click next. Now, you're in a sreen with TABS. The first tab is for adding tables as entities to the model. The second tab will its for update the model. Select the tables and views you need to add/update and click next. That's all.
I am using ADO.NET Entity Data Model in my C# project. There is an ADO.NET Entity Data Model that is generated on the DB in my project. How do I quickly refresh the ADO.NET Entity Data Model based on DB changes? I have been deleting the model and then creating a new one. I believe that there is a more simple and quick way.
Right-click on the model designer and choose "Update Model from Database", and a dialog box will pop up. Click OK and your model will be refreshed.
FYI, I'm using EF 4.0, and I skipped the earlier version, so I can't tell you what it's like in VS 2008.
Update Model from Database doesn't work very well. At least in VS 2008/C# 3.5. I fails to remove old columns, etc.
I always Ctrl-A (to select everything), the press delete, and then create model from database from scratch - update just doesn't work right in a lot of cases. At least in EF4 / VS2010 that I am developing in/with.
Perhaps this helps... Update Model Wizard (Entity Data Model Tools)
You should open your Model.edmx after that right click on the properties. now select the (Update Model from Database) and it will going to show you another panel with tab panel. you can choose Add,Refresh and delete. in your current situation, you should select Refresh. then select Tables->dbo->and name of your table in database and then click finish. that's it. you've refresh your table.
but sometimes you can see in your model that something went wrong because the column is not in right position. to solve that just right click the column name and select 'UP' to move it up or 'DOWN' to bring it down.
i hope that it will help.
Right click - Update Model from Database.