MVC scaffolding is great, until I need to extend my database schema - asp.net-mvc

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.

Related

MVC5 option group maintenance table for CRUD

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.

Recreate database view using migration

Is it possible to recreate a database view using a migration? Migration that was used to create a view on database is no more as per the changes done in some table structures. How do I recreate a view as per the new design? Do I have to drop the existing view and create a new through the migration? Or is there a simpler way without losing data?
If you're talking about a view stored in the database, the usual technique in Rails would be not to do that but use scopes in your ActiveModel instead. This next statement is more with MySQL specifically in mind, but probably true generally: as long as you have the indices properly set up, that approach should be as fast as having a view in the database itself and be more programmatically easy to work with. You can specify calc'd fields in a scope through SQL statements if appropriate, though the usual response to that idea is why not have that be an accessor function in your model (as if it were in the database).
Does this help or am I answering a different question than you had?

MVC Instant Automatic Scaffolding of Entire Model (like Dynamic Data)?

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.

Should I make a new Class in Rails for Redis?

I'm starting to use Redis, and first thing my code is not too DRY, and was going to consolidate it in the application.rb and controller. Is this the best way to go, or should I make a new Class called Redis, and have all the logic in there?
My models are currently Customers, Orders, Products, and I'm using a lot of counters.
You will probably need a combination of new and existing model classes.
In many cases you can just drop the model used by the view directly into the datastore, which saves repetition. However, there will always be some places where the needs of the view and the datastore are different.
For example a property that appears as a list of values in the view may need to be stored as a separate set key rather than serialized with the other properties of the model.

EF4 Code First create new table

Is there a way when i add a new entity to my Code First configuration the table gets automatically added so i don't have to worry about updating my DB with new tables?
you can add
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourDbContext>());
in you r application start . It will recreate database for you if you have modified your configuration.
And if you do not want to drop and create database (To incremental development) you can use SqlMigrations. http://www.hanselman.com/blog/EntityFrameworkCodeFirstMigrationsAlphaNuGetPackageOfTheWeek10.aspx
From: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx
Setting an Initialization Strategy
In the next section we are going to start changing our model which in turn means the database schema needs to change as well. Currently there is no ‘out of the box’ solution to evolve your existing schema in place. Database evolution is something we are currently working on and a sample of the direction we are heading is provided in a recent design blog post.
There is however the opportunity to run some custom logic to initialize the database the first time a context is used in an AppDomain. This is handy if you want to insert seed data for test runs but it’s also useful to re-create the database if the model has changed. In CTP5 we include a couple of strategies you can plug in but you can also write custom ones.
Add a using statement for System.Data.Entity.Database at the top of Program.cs
using System.Data.Entity.Database;
For the walkthrough we just want to drop and re-create the database whenever the model has changed, so at the top of the Main method in my Program class I’ve added the following code
DbDatabase.SetInitializer<ProductContext>(
new DropCreateDatabaseIfModelChanges<ProductContext>());

Resources