Updating MVC3 application after altering database tables - c#

Thanks in advance for your time :-)
I am working on an MVC3 application and a related database project in visual studio 2010 professional. I am switching between the projects as I revise database structure.
I'd like to know are there any tricks for updating the controllers and views in my mvc application? Updating the model (edmx and tt files) is a piece of cake. but it seems to be a huge chore to recreate the model and views without simply deleting them and starting over.
Deleting and starting over wouldn't be too bad if it didn't cause me to loose customization of the views and wreak havoc with my svn system...
Thanks again for any thoughts!

No, the code generation templates are meant to be run from the start, they are not intended to do change management of your controllers and views. If you have a standard template that you want to use, you can always use the T4 templates to create your own that will generate code that is closer to the end result you want.
But that is your only option, other than coding it by hand.

MVC Scaffolding would allow you to quickly re-generate views, controllers, repositories etc. However you would lose any customisations.You can customise the T4 templates to make it make pages more like you want. If your changes were generic that you always did then keeping them in the T4 template would mean they would also be regenerated by the scaffolding.
It is a good way to quickly add new functionality/or replace existing.

Related

c# working with databases rather than use visual studio generated code

Well I have been programming odd applications in C# for awhile. I am fairly adaptable to any language. That being said I am by no means an expert. I can get done what I need to and it is fairly clean. The one area that perplexes me is working with database. I understand how databases work and even write some SQL queries. The biggest trouble I have is I use visual studio to auto generate code and it was mentioned in a previous question to stop using that. Well the time has come that I believe it will be beneficial for me to learn the basics of connection to a database and creating the auto generated code that visual studio usually handles. Can anyone here provide me with a place to best learn this? I want something that will guide me on best practices for performance and clean up not just how to make it work.
Thanks in advance
If you want auto-generated code, are you wanting Entity Framework Code First? I noticed you didn't specify.
This is the official documentation for Entity Framework.
https://msdn.microsoft.com/en-us/data/ee712907
I'd focus on the Code-first portions and probably don't even worry about the EF Designer, Model First, or Database First in there.
Some key concepts you want to make sure you understand.
Code First
Fluent API
Scaffolding
Data Annotations
Migrations

How to add a model in asp.net mvc project

I recently made my first ASP.NET website with MVC. I selected the option that pre-loads a basic project with login functionality and a few pages. I spent a couple hours learning how it all works and I'm pretty comfortable with most of the features. Now I want to add a class that interacts with the database and I've run into a bit of an issue.
When I search for a solution every response says to use DbContext. I don't think there's necessarily anything wrong with this, but when I search for DbContext and some other commands that show up frequently in these responses, there are no instances of them in the project. I would really like to use the same method of creating models that was done for the account classes, but when I look at the code I'm not entirely sure what I'm looking at as it links to a bunch of different files.
Can I get some tips on how to create classes the way that ASP.NET creates default account models?
For reference: I'm using Web Essentials, Productivity Power Tools and VS 2013.5
There's tons of tutorials online that cover ASP.NET MVC Code First Entity Framework. I start with something like this
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
And see where you get to. This is the basic approach:
Create the model classes
Create the Context
Enable Migrations (in case you need to change the model)
The SQL Expres database will be built automatically based on your EF classes.
Good luck. It's not a difficult as it first appears once you've done your first one.

How can I update aspnet-scaffolding cshtml pages

I am using MVC 5 controller with views,using entity framework and create, delete, details, edit, index cshtml pages occurs automatically. When I change the model classes and update the database CSHTML pages don't change. I am manually deleting view pages and recreating them with scaffolding. When I do these things my HTML designs are deleted.
How can I update aspnet-scaffolding cshtml pages automatically?
Note: I am using layout pages but they aren't solving my problem.
"When i do these things my html desings are deleted. "
Here is the root of the issue. If I understand you correctly you are:
Using scaffolding to generate Views.
Modifying the CSHTML of those views to customize the View.
Your hope is that after you modify the database, the scaffolding will update the views. However, when it generates the new CSHTML Views, how does it know how to combine the changes with your customizations? It doesn't. It can't know how to generate a completely new view and somehow integrate your customizations.
This is pretty much the same with any scenario that involves code generation. You have to make a choice. If you want to preserve your edits, you can no longer use the scaffolding to generate new views after editing. If you want to benefit from scaffolding after database updates, then you should never edit the CSHTML with the intention of customizing it.
I used scaffolding a long time ago only as a learning tool, to see how things like lists were handled. For probably 2 years I have never used scaffolding, and it is generally slow for larger projects, so I just always create an empty CSHTML or copy an existing one that is close to the approach I will take with the new one.
I would instead focus on other techniques that will help make creating CSHTML easier.
Using partial Views appropriately to pull reusable UI elements out.
Use Custom Object Templates to customize the results of things like DisplayFor and EditorFor.(I personally don't do this but I've seen others use it effectively)
Implemented helpers to take in parameters and produce HTML that fits the patterns you follow. Since I use bootstrap I created my own Html.LabelForBootstrap helper for example, that outputs HTML formatted for use with bootstrap with bootstrap classes.
Effective use of Layout pages and sections.
Eventually you will begin to see that the scaffolding isn't at all appropriate in many cases. E.g. the UI for relating one entity to another, the fact that it shows all columns when many aren't appropriate for display, etc.
You might be interested in Dynamic Data. It is more convention oriented in that it determines UI from database structure, but you provide customization conventions. Overall it's not really different conceptually from using MVC with Custom Object Templates:
http://msdn.microsoft.com/en-us/library/vstudio/ee845452(v=vs.100).aspx
Dynamic Data isn't mainstream, and skills you learn from using it probably aren't very marketable. It is probably only appropriate for administrative interfaces where user experience isn't a primary concern. Once you learn MVC really well you can pretty quickly spit out basic UI for new entities, so I don't recommend Dynamic Data.
It is possible to modify the default scaffolding templates. So if you wanted to, you could customize the template and have it generate your views. You will quickly run into the same issue however, as you will encounter a scenario where your generic template doesn't satisfy and you need to customize the output View CSHTML, but now you can no longer regenerate the View so the scaffolding template has limited usefulness:
http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx

The difference between building a Rails application and an ASP.NET MVC Application?

First of all some background: 4 years of C# experience and a year of Rails experience. I would appreciate any input from someone who has worked in both and knows a lot about developing ASP.NET MVC applications.
I've been doing nothing but Rails projects for the last year. Now, I have a client wants their application converted from ASP.NET Web Forms to ASP.NET MVC. This is the first time I've done MVC in C# so I'm trying to see how different things are and if certain productive Rails tasks map over to ASP.NET MVC.
First of all, is there such a thing as a Scaffold in ASP.NET MVC? I see something called an Area but I don't know if thats quite what I want. Also, how can I generate a scaffold (models, controllers and views), just a controller or just a model based on the same information I would give a Rails app?
For example I might do something like:
$>script/generate scaffold person first_name:string last_name:string
which produces a Person model, a migration script (what I run to build the database table), a People controller and views for each of the RESTful interfaces (index, new, create, edit, update, show, destroy). Can I do something like this in Visual Web Develop 2010 Express?
There is MVC Scaffolding with MVC3.
Here's a nice post on it.
Whereas Rails has tries (especially for beginners) to guide you into one way to write your app, MVC attempts to be all things to all people. So it's very flexible, but it's hard to specify the "one true way" to scaffold something.
So one way which works is:
Create your DB. Create an Entity Framework model in the usual way from the DB.
Compile.
Right-click Controllers, Add, Controller. Check the box for actions.
Right click one of the generated actions, choose Add View.
Check the box for "Create strongly typed view" and select scaffolding from the combo box.
But there are many other ways!
There are 3rd party tools for migrations, but nothing built in. What is built into full VS (maybe not express) is database comparison and merge script generation, an arguably more powerful, but perhaps harder for new developers to understand, alternative.

Generate simple web site GUI using .net

I have a couple of super simple databases. Basically they all consist of single tables that saves tracking/logging data etc.
Now I want to list this data but I'd like to find a solution that's applicable to all the different tables in all the databses. SO basically I'm looking for some way/some pattern of pointing a solution to a database, generate code and GUI and the publish the site. The tables can have huge amount of rows so I need functionally like paging etc but otherwise a simple list is all I'm looking for as a first step. I've been looking at Dynamic Data from MS - could this work? Other options? I'd really like the web sites to me ASP.NET MVC ...
As a seconds step I'd also like to have the possibility to change/add and delete data from the rows via the GUI.
Consider using ASP.NET MVC 2. It has extensive scaffolding capabilities that can auto-generate the views for you, yet allows a lot of fine tuning
Brad Wilson explains scaffolding here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
For MVC 2, see
http://aspnet.codeplex.com/
and
http://www.asp.net/mvc/download/
Paging is not included, but you can get that from MVCContrib, for example
You could roll your own scaffolding and generate the actual pages using T4 (http://www.olegsych.com/2007/12/how-to-create-a-simple-t4-template/ and others), or you could look into monorail (http://www.castleproject.org/monorail/index.html).
If you don't mind mandatory JavaScript then I would suggest jqGrid. It should be very simple to enumerate tables, columns, and generate jqGrid's columns from it (colModel). You can see an example of how I do a similar thing here - it does almost exactly what you need but for classes, not database. So, you can't use the solution "as is" because it's for domain classes, but it is very close, you can take it as a base and rewrite JqGridExtensions.JqGridModel to process db table definition, not domain class definition.
If your site has to work without JavaScript, then either MVC v2 with scaffolding will work, or you can try S#arp Architecture which also includes its own scaffolding out of the box, that generates full CRUD model/controller/views/repositories/etc. The benefit here is that you will still work with classes, not datasets. However as far as I understand you'll have to define scaffolding manually for each entity - but since scaffolding is very flexible, I'm sure you can write Uber-Scaffolding class that will enumerate your database and spit out sub-scaffolding files or just call them right away. A quote:
you programmatically create an object
called EntityScaffoldingDetails which
holds details such as the entity name,
the namespace, along with property
details such as the property name,
type, domain signature inclusion,
attributes, default test values, and
SQL generation details.
But, I better like the jqGrid way, because it's very easy to generate jqGrid model definition from the database schema which usually contains all the metadata. Of course, one can integrate S#arp scaffolding and jqGrid so that the first produces the latter. The benefit of scaffolding is that you have full control over the code, you can take it and tweak it. Because, you can't expect it to be 100% automatic - generator can't decide HasMany or ManyToMany, control type, hidden fields, etc. Of course you can use SQL metadata (properties maybe) to give hints for your scaffolding generator.
Now, if you need a ready out-of-the-box solution, I don't think there's one except for maybe commercial tools. The above will only allow you to assemble your own one. As for commercial tool, for example, Telerik Grid said to be able to "automatically infer its columns based on the properties of the data item it is bound to".

Categories

Resources