Refresh Controllers/Views after Database Change in MVC4 Database First - asp.net-mvc

My DBA decided to rename some fields in the database, so I refreshed my EF data context.
Now I'm wondering if I need to delete / re-create my controllers and views or if I can 'refresh' them without deleting (since I've made modifications to my controllers). Either option would be faster than hand editing in the changes, since there's quite a few.
Thanks for any advice.

Unfortunately, it can't be... The model can only update entity classes, not views and controllers.
A trick for could be to creating a new controller with a new name and keeping the old controller as well. Then, copy those important codes from the old contoller to the new one.
And the same approach for views

Related

ASP.NET MVC - How to use code first and database first method in same project and with same context

I have a requirement that I have to use code first pattern (which I have to implement) along with database first (which exists in the current system).
Now the condition is I must not create a different context for new tables or any other changes which I make in the database but have to maintain current context from DB first pattern. Is it possible to create code first and DB first in the same project sharing the same context? Do I must manage .edmx file or is it possible to handle the database from code first pattern only? And that too with managing TransactionScope.
I need some suggestions on this.
There are some things that I learned from my above problem.
One cannot use the same context for Code first and Database first.
To use code first and database first in the same project, context must be different and irrespective of ConnectionString. (one can use either existing ConnectionString or make a new one)
It is not good practice to use two patterns at same, but if situation demands it, than one must have no choice. Therfore at last one can create POCO classes for code first, from database first so it can be useful.
TransactionScope be used with irrespective number of database connections and it will work properly.
If anything I am missing, than please add it so other can have better idea, or they could at least save their time.

Add existing database view/table from object explorer to entity diagram

I have created a new asp.net 2012 mvc application. I am moving an existing web forms application over to the mvc model. The web forms application uses linq to sql, but since it's no longer going to be supported, I am going to use linq to entities.
Having never used mvc or linq to entities, I have been reading every article I can find.
I added the ado.net entity model and generated the diagram from my existing sql server 2005 database. I was having problems in the .edmx diagram with a relationship so I deleted the view expecting to be able to drag it back to the diagram.
I have looked all over on adding an existing database view/table to a diagram with no luck. I don't want to regenerate it, I spent a lot of time modifying relationships etc. in the current Diagram.
How do I add back a view/table from a database to an existing diagram?
Thank you,
I was afraid the update model from database option would change what I had done in the model, so I avoided it.
After hours of searching, I decided to try it and duh... it let me pick what I want to add.
Right click on your .edmx file and select Update Model From Database. Check what you want and that's it.

ASP.NET MVC Database Views

Quick question about database views. Am I right in assuming that I can create a database of view of various tables and connect them how I want etc and then when I do queries, add, edit delete etc then MVC will figure it all out for me without needing to do any complex SQL in the controller or repository?
Odd question but just wanted to make sure my assumption was valid. Cheers
Unfortunately, MVC will not figure it all out for you, you'll still need to write the SQL code (or use an ORM framework) to communicate with the database.
What MVC gives you with it's architecture is a clear separation of responsibilities:
Views are responsible for displaying data and should be as simple as possible (i.e. little to no logic in them)
Model(s) contain the business logic and rules
Controllers are responsible for passing data between the Model and the Views.
What you are looking for is Scaffolding. In .net MVC I can't think of any tools which do this for you directly against the database. They all require either as Russ said an ORM i.e. Linq To SQL or Entity Framework (EF).
http://msdn.microsoft.com/en-us/library/cc488540.aspx
The closest you could get would be to use Database First model generation and then put the necessary MVC templates/views/code on top.
A database view is read-only so you will not be able to perform write operations on the view. You can however create a model from a view and display your data as defined from the view. If you are using an ORM solution such as ADO.NET Entities you can instantiate an object and add the child objects to it and be able to save the final result in a single transaction as well.

Can EF4 generate POCO for me, or do I have to write them myself?

I've been playing about with the Entity 4 framework lately and it's pretty nifty. I've setup a class called Customer.cs with some properties like Name, Address etc. I also have a class called StoreEntities.cs which binds these back to the database through DbSet. It works fine and I can pull all my customers from the database.
The problem is every tutorial I come across on the internet generates their classes by hand. What I mean is, they all say something like "Now I'm going to make a new class called Orders with the following properties" and then proceed to write it out. That might be ok if I was starting a new project, but I'm porting over my old website so I have upwards of 20 tables in my database. If I go through and write all these out by hand I'm going to be there all week :D
Plus I'm not sure what would happen if I made some changes to the database (since I would have to go back through and update all my classes by hand).
I was hoping EF4 would have something similar to a Class Diagram, where I can point it at my database and have it generate a bunch of classes for me based on that.
I'm a little lost on this. Am I going about this the right way?
You should take a look at the ADO.NET C# POCO Entity Generator. With that you should be able to generate your model from your existing database and T4 templates will generate your POCO classes based on your databases tables, etc.
Here is a link to MSDN that explains how to Update Model from Database using the Update Model Wizard.
There are two ways to achieve what you want. Both start from adding new item to your project. In the "Add new item" window select "ADO.NET Entity Data Model". There you will have two options. One to generate your model from existing database and second to create model manualy in designer. If you choose first one you can simply drag and drop tables from server explorer to design surface and all your entities and their relations will be generated for you automaticaly. Also you can modify that generated model later.

asp.net mvq regenerating autogenerated view code after database table update

i have generated the views (Create, Edit, Index, Details) for a table using LINQ to SQL.
My question is:
Once the views are created in Visual Studio using "Add, View", they dont change when i update the database (using Server Explorer) and the LINQ to SQL code.
Is there anyway to "Refresh" the view code, or do i just have to delete the existing ones and create new ones?
I see what you mean and as far as I can tell there is no way other than to begin again.
Having said that though any changes you make to the LINQ to SQL classes I would imagine be fairly minor so coding them wouldn't really pose that much of an issue.
I wouldn't, personally, delete and start again when I add fields I'd simply code in the changes as generally speaking I've modified the auto generated views to reflect my styles and layout.
Using the AutoGenerator is great for doing a lot of the grunt work up front but after that you're kinda on your own I think.
It might be handy to post a few lines of code here but here are the things I would check;
Is the database being updated
When you return your view after a postback are you reloading the data?
Are you doing a JSON postback and not handling the callback event?
After the submit are you returning the FormViewModel with the new data in it?

Resources