Build ASP.Net application with Web API and database first strategy

asp.net asp.net-mvc asp.net-web-api crud entity-framework

Question

I am new to ASP.NET MVC 4 and Web API.

What I want to achieve is to create a CRUD web application that is able to manipulate the data tables in a simple existing SQL Server 2008 database.

I thought about the new MVC 4 with Web API and Entity Framework. There are a number of samples and examples about code first data access pattern but very few about database first.

Can anyone help with any brief idea how to achieve this with database first and Entity Framework and repository pattern please?

1
5
11/16/2012 1:04:41 AM

Accepted Answer

I started down this path a few months ago: learning ASP.Net, MVC3, using an existing database to build an App.

This is what I found (I'm happy to be corrected):

  1. Don't learn VB, learn C#. There are very few VB samples around.

  2. I followed a 'database first' tutorial. There are many tutorials on the web, just get started and follow one and don't be afraid to start over

  3. If you want anything remotely flashy you need to use JQuery - this is basically a javascript library. MVC / ASP.Net offers very little in the way of interactive grids and pages.

  4. It turns out that MVC is a bit of a misnomer. Often you need 5 layers, not 3:

Model (The M in MVC, generally generated for you by some code generation tool like Entity Framework, maps directly to tables)

ViewModel (wrapper classes around your autogenereated table classes that add more useful data) - this post is where I came accross them: MVC dbContext find parent record when current record has no elements

Controller (The C in MVC)

View (The View in MVC)

Javascript (If you want anything beyond a basic HTML form, like a gird or a date picker you need to use javascript)

Like I say I'm happy to be corrected on any of these points. This is just my viewpoint at this stage of my journey. I have to say I have only investigate jqGrid as a grid solution and I'm just about ready to try something else.

4
5/23/2017 11:54:36 AM

Popular Answer

What you've described (CRUD operations, SQL Server, Entity Framework) is the assumed default for MVC4 projects. This should be very straightforward for you to set up given a database-first approach.

  1. Create the MVC4 project in Visual Studio
  2. Under the Models folder, create a new Entity Framework class (ADO.Net Entity Framework Model). Choose "generate from database" and follow the instructions
  3. Build the project
  4. Right click on the Controllers folder and add a new controller. Choose "MVC controller with read/write actions and views, using Entity Framework". For the Model class, select the table entities you want to target. For the Data context class, select the Entity Framework class you created in step 2.

That's it. You should be able to run the project and have scaffolded CRUD forms fully operational (navigate to /YourControllerName to see a list of rows from the table). You can repeat step 4 as needed to add other table controllers.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow