Does MVC 3 have a way to automatically scaffold controllers and views for my entire code first model? For example for each of 70+ classes that I have assigned a DbSet in EF 4.2 code first? Or do I have to re-scaffold between 1 and 100 classes every time I change my huge data model?
I just about switched over to using Dynamic Data for this functionality but I think I'm changing my mind again. Too many errors and performance issues. How can I achieve Dynamic Data sweetness in MVC?
I had created a recursive object template before and was using attributes on the code first model to control the rendering. That's not necessarily what I'm looking for though. Just a way to quickly scaffold controllers and views for 70+ classes over and over and over again. Then with EF migrations and some voice command software I can work in a hammock maybe..
EDIT: I found this similar post here. Now I have to learn what powershell is I guess? Then buy a hammock?
I used reflection to get a CSV style list of the types in my DbContext's DbSets. Then used MvcScaffolding from NuGet in the Package Manager console to foreach through them and scaffold controllers referencing my existing context type.
PM> $Types="WindowStyle", "WindowSize", "WindowPreset", "WindowGridColor",
"Window", "VinylSidingColor", "VinylShutterColor", "VinylFlowerBoxColor",
"TrimMaterial", "ThirdPartyService", "State", ....
and then
foreach($t in $Types) { Scaffold Controller -ControllerName $t -NoChildItems -DbContextType MyContext -Verbose }
I think I might have to watch for pluralization issues when scaffolding the views.
Related
What is the best approach for maintaining (CRUD) on an optiongroup list that is driven by two sql server tables?
The option group is like this and is driven simply by two tables (parent child)
OptionGroupOne
ItemOne
ItemTwo
OptionGroupTwo
SecondItemOne
SecondItemTwo
etc…………….
Was thinking of just looping through in an unordered list?
I’m using entity framework 6 with MVC5 don't really want to use javascript.
This is a long way if you don't seem to have anything ready yet... I bet that's the reason why no one wants to start explaining.
You should organise the Lists in nested ViewModels, the OptionGroupViewModel and the ItemViewModel. The OptionGroupViewModel has a string property for the name and a List<ItemViewModel> property for the children. So the main model should be a List<OptionGroupViewModel>.
In your cshtml, Using the Html.EditorForModel() extension method on a the latter, you should get some View Results already, the EditorFor... methods are quite intelligent and will generate views based on the data structure.
Now you will need a MVC Post method with the List as the model and you will find all updates there, You need to map them back to the Entity Framework model (or whatever kind of base model you have). As you mentioned in your comment above, you want just a "way of editing the titles of each option group and the title of each group". This would be pretty simple but is half way to CRUD, and Delete + Add are much trickier in that scenario.
Using frameworks like Knockout JS is a big gain here but requires some learning and will introduce JavaScript to your project. The linked tutorial is for Web.Api but it will also work for MVC.
Using EF 4.1 in MVC 3 environment. I'm also using the POCO generation tool I downloaded using NUGET.
I am looking for a way to "customize" the POCO classes with attributes for validation without losing these changes every time the database changes (and a resulting re-sync is performed).
I've tried creating abstract classes and instantiating an inherited class, but EF forces me to create a concrete class through the EDMX file and this descendant class also becomes a generated POCO which is "refreshed" with every database sync.
I've notice the POCO's were partial classes meaning I could add members to the classes in a different file, but this approach wouldn't let me add to existing members.
While I understand that what I'm running into is a limitation of the database first approach, I suspect that there is a way to alter/customize the POCOs in a way that isnt lost with each re-fresh.
You have at least 2 options:
Implement the IValidatableObject interface on your partial class and provide the Validate method.
As Eranga mentions, use the MetadataType attribute to move the validation attributes to another class with the same properties.
Overriding OnModelCreating will only work for code first and isn't an option in model / database first.
I'm using a code first approach to a simple web application: currently just a single table of book reviews.
I modified the model to include an extra column ("Rating"), and I also have an initialiser which correctly rebuilds the database every time I change the schema.
The problem is that none of the CRUD Views are updated to reflect the new "Rating" column. Do I have to modify each View by hand, or is there a simpler way?
Yes, you have to manually add them. Scaffolding is intended for simple set up of views / controllers only.
I'm trying to introduce ASP.Net MVC to my department. I am encouraging them to have a ViewModel per View and AutoMapper for our larger projects (and ideally in general).
Sometimes this means having one large entity and picking 5 of its properties and creating the ViewModel. This is done by looking at the the edmx model (many projects were existing so it was DB first) and then creating matching properties in a ViewModel class. Obviously names etc have to match for AutoMapper to work. Also for navigation properties you have to add the navigation name first. Ideally also being able to type in a display name etc.
I'm trying to ease them into doing this (what they see as extra work). Is there any tool that would load a list of fields and allow you to select via checkbox etc and create the class from that?
I guess the same would apply to DTOs etc
Thanks
Have you tried this http://www.codesmithtools.com/product/generator
you can create a template of the dto and then it will generate the files/dto's for you as needed from any kind of datasource.
I have a load of ADO.NET Entities in my MVC project. I was going to use these entities directly from my views/controllers... however, I think it's probably best to use ViewModels which more accurately reflect what the View requires.
I'm looking for a way that I can auto-generate a ViewModel from an existing Entity, i.e., auto-generate the wrapper or adapter pattern from an existing member... Or a T4 template that would loop through the public properties of an Entity, and output properties for the ViewModel... then I can delete the properties I don't need or create aggregate view models etc.
I cannot seem to find anywhere a way to auto-gen a wrapper or adapter pattern class from an existing type?
The idea is then at runtime, use AutoMapper to map between the ViewModel and the Entity.
thanks
You could use AutoMapper to convert from your domain model to a view model. There's a great post from Jimmy Bogard explaining how you could integrate this within your controller actions.
http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx
That can help. It is actually for metadata generation for existing entity types. But you can use it to generate clean view models with data annotations as well. Maybe with a little modification.