Simple MVC 3.0 Razor EF4.1 Code first web app…but really still DB first

Start by downloading both MVC 3.0 and EF from Micrososoft

Then Open VS2010 and create a new MVC3 project

Now we Point to a db… right click on Models folder Add New and then select a EFdatamodel

In this case we are generating from a database.  If you have a connection string you can select it from the dropdown, if not you will have to create it.

Right click on the Model in the designer and select “add code generation Item”

Then you select ADO.Net DbContext Generator

This will generate all of your modes including the dbcontext.

Then you will need to “build”.  If you do not build at this point your models will not pull into the controller.

Now when you go to make the Controller for an entity (table)

Now we have all of this backbone to pull from already added to the form. Our model and the data context class

I selected Razor because i wanted to check it out.  You an also switch views to be traditional aspx.  The syntax is slightly different and very different in some areas.

Now when I created the Controller it actually made all of the corresponding views.

They are quite generic, but they are there.  Used the Index as the first page which is the list.  There is a “new” already there that takes you to a new entry screen, an edit and a delete…

Super simple!!!

Now I need to make a change to my database and have the code regenerate.  This is not TDD, I know slap on hand.  Actually at this point I am pretty much winging it.  I was bragging  I could make a simple contest app in a couple hours and here I am blogging about it.

Pretty simple, I continued with my DB first concept and added the field in the db, then of course after I rebuilt my code realized I want it in another table.  I noticed on the refresh of fields from the table it was more willing to add elements than remove.  I had to manually remove the field from the model designer for the field I didnt want but it did still regenerate all of my classes correctly.

Diccussion about defaulting a field to todays date by default on a form with MVC 3.0


@Html.TextBoxFor(model => model.dateentry, new { @Value = DateTime.Now.ToShortDateString() })

Does not work

@Html.EditFor(model => model.dateentry, new { @Value = DateTime.Now.ToShortDateString() })

SO now I need a chart:

Has a great blog about it.

I only used the chart writing code.  I may go back and add the other code if there is a need.

You end up having to set the date to string inorder to get it to show up in the text box… GRRRR 2hours

Something else that I found once working with adding additional tables.  On my first creation of the db I used “id” as my uniqueidentifier for each tables primary key.  This is apparently a standard.  I like to name my primary keys a little more verbose, so if you do not just use “id” it does not The views created with Razor from the controller will not automatically add the GUID on insert.  It does not appear to be an issue either way if you are using identity which I almost never use anymore.

Moving to PROD

Moving the application to IIS 6.  First I needed to install the MVC 3 on the server.  Then I did end up having to add the .mvc  file extenstion mapping and still had to add the rerouting in the global.ascx to add the .mvc

I could very well have something set up wrong. But the app is up and running with really no issues.

About dorothyjarry

Super Dots
This entry was posted in Coding/Development. Bookmark the permalink.

5 Responses to Simple MVC 3.0 Razor EF4.1 Code first web app…but really still DB first

  1. Julie Lerman says:

    Hi Dorothy,
    nice job but I’d be careful about using the term “code first” here. You are definitely using an EDMX not code first. CF is when you do not use the designer. The beauty of what you’ve done is that you get to leverage the existing database and use a designer PLUS use the new DbContext that is where you get the nice scaffolding magic in MVC3.


    • dorothyjarry says:

      Thank you for your comment Julie, It was actually intended to be a play on the word of the methodology and you are correct. This is a Database first application. Old habits die hard. I am hoping the point that “this is a very flexible and easy programming platform” comes across.


  2. Julie Lerman says:

    p.s. razor is KEWEL, isn’t it? 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s