How to handle database changes - asp.net-mvc

I am using Entity Framework with a database-first approach, and I made some changes to the database. I cannot see those changes in my application because I know I need to update it too. But how do I update my domain models classes in ASP.NET MVC? Please can anyone help me?

Open your edmx file and Right click on your edmx diagram and select Update model from database
2.if you need to add/refresh the existing tables,select the tables under add/refresh button and finish
save the changes and build your solution

Related

Entity Framework Code First from database + MVC

I'm currently working on an ASP.NET project which I've not done before, and I need to use Entity Framework to access a database. I have a database already and following along with this tutorial: https://msdn.microsoft.com/en-gb/data/jj200620
I've managed to get Entity Framework to create the classes for the tables in my database but it has created them all in my Views/Home directory like so:
But from my understanding of MVC these should go in the Models directory? Is it safe to just cut and paste these generated files into Models or am I going to run into problems in the future because it's going to still be trying to use View/Home?
It should be safe to move them to Models. The models are compiled, not deployed as website content, so ASP.NET does not care about their exact location.
You will probably want to update the namespaces of the models, though. I'm guessing that the models were created with a namespace like YourNamespace.Views.Home, and you will want to change it to YourNamespace.Models.
Also make sure to update the namespaces to any references to your models (if you have any). You will get compile errors for any references that you missed EXCEPT for any references in cshtml files.
It would be a lot easier to just delete everything created by EF, and add your ADO.NET Entity Data Model (.edmx file) again into the right folder.
On step 3 of the EF guide in your question, when you add the ADO.NET Entity Data Model, make sure you add it into the Models folder. (Right click on the Models folder, then Add New Item...)

An issue created due to experimentally selecting wrong scaffolding option in EF5 , MVC4 application

I am using VS2012 and EF5 first time. I developed application just like MVC3 , EF4.1 application without using any new feature. I already have a database , i created db context
, models , controllers and views. Every thing was working fine. Then i created a new table Directorate in database , created its model and database mapping in dbcontext.
While creating its controller i selected "MVC controller with read/write actions using Entity Framework" just as an experience to see what will happen. I selected my context
and model. When i run the app it created a new table in db with the name DirectorateModel and also inserted mapping in context. As i already have created table and mapping so i deleted each new functionalities added due to this action i.e. i deleted table, context mapping , controller and views.
but now when i run the application then it gives error:
The model backing the 'ArchievingDBContext' context has changed
since the database was created. Consider using Code First Migrations
to update the database
(http://go.microsoft.com/fwlink/?LinkId=238269)
I visited the mentioned url but it is about code first migrations that i don't want to use. Anyone please help me how i can fix this issue, i think i hagve to delete something
that was created due to selection of wrong scafolding option.
I just want my application to run in previously working state.
you just need to set correct database initilizer:
Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists())
and this work
You can fix it by calling:
Database.SetInitializer(null);
in the Application_Start method of Global.asax
You don't want to be initializing on top of an existing database like that.
See: This Question
Remove the Directorate entity from your .EDMX model and the controller/views you've created using scaffolding on it.
Change your database as you want and then go to the VS Model designer (just open your .EDMX model) and Update model from database and save changes.
Now you can add views/controllers by scaffolding without any concern...

in Asp.net MVC, Do I need to update Entity if I change my view column?

I find this site is very interesting and very helpful. I came up with this doubt while programming a project in asp.net mvc. I had to remove few columns from a view which I was using to pull data from the database back to my presentation layer. After removing those columns, I build my project and there were no errors. When I ran it, my project broke with error saying missing related columns as Entity was not changed.
But, my question is that is it necessary that every time I need to update my entity model?. What if I deploy my project in production and then had to remove one column from the view? It will be a hell of task for me If I had to update model, build and deploy it back again on production. Is there a simple solution for this?? thanks for your help
You have to update the entity model every time the database structure changes, what you need to do is to decouple the EF model from the MVC project, if you don't what to work with Ioc, a simpler approach whould be to place the EF model in a separate assembly, when the database changes, rebuild the assembly and deploy it on the server, this way you don't need to rebuild the entire MVC.

Code first database on Entity Framework 5 in another project

I've got an ASP.NET MVC 4 site with MSSQL database generated by code first approach. I want to use this database in another project. This project should crawl several tables from db, update them and send notification to users.
What is the best way to add existing database from the first project to the second project? I'm thinking about generating edmx by database, but this approach doesn't seem good enough.
You would be best moving the EF code and Entities into their own library, and then having your two different projects (Web and Windows Service) have a reference to that library.
Then, if and when your database structure or entities change, you only need to do this in one place.
If the second project is not going to modify the structure of the database, then I would use a data-first approach in your second project.
You can use codefirst mapping without problem on existing database. So simple reference assembly with datacontext in other project and it will work. (Don't forget set connection string in config file of other project)

refreshing just one entitiy in Entitiy Framework 4.0

Is it possible to refresh only one entity (which depends on one table) in entity framework 4.0 designer?
when I refresh model from sql server database, it refreshes all entities.
Thanks in advance.
No you can't refresh only one entity at a time with the update model from database wizard.
You can make a backup of your edmx, update it, open it as an xml file (right click>Open With>XML (Text) Editor), copy the modified sections about your entity (in the csdl, msl and ssdl sections), and paste them in your backup.

Resources