Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 347
alextc Avatar asked Nov 16 '12 00:11

alextc


People also ask

What is database first approach in ASP NET MVC?

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.

What is ASP Net Web API with example?

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the . NET Framework.

Is ASP NET MVC easy to learn?

It's really difficult to try and learn an entirely new language/framework under pressure. If you're required to deliver working software for your day job, trying to learn ASP.NET Core at the same time might be heaping too much pressure on yourself.


2 Answers

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.

like image 192
McGarnagle Avatar answered Oct 12 '22 11:10

McGarnagle


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.

like image 41
Nick.McDermaid Avatar answered Oct 12 '22 10:10

Nick.McDermaid