sankeats
sankeats

Reputation: 13

Connect to SQL Server from ASP.NET MVC application

I am entirely new to ASP.NET MVC. I have one ASP.NET 2.0 Framework Web application with below architecture

I am moving the application to an ASP.NET MVC 4 architecture; can anybody suggest the best practices to go with for data access layer, assume the connection string will be in web.config?

Code-first? Or data-first approach? What is the difference with the above approach and Entity Framework?

Also while adding a controller for a model, amongst the below template which I need to choose?

  1. Empty ASP.NET MVC controller
  2. ASP.NET MVC controller with read/write actions using Entity Framework
  3. ASP.NET MVC controller with empty read/write actions
  4. Empty API controller
  5. API controller with read/write actions using Entity Framework
  6. API controller with empty read/write actions

What is the difference between the above templates?

Upvotes: 1

Views: 1437

Answers (2)

sankeats
sankeats

Reputation: 13

Okay, I have sample demo project which meets my requirement for data access layer, but i am not sure what approach they have implemented in that sample project. How to find the existing designed data access layer in MVC? Whether its Code/Data first approach and controller templates used?

Upvotes: 0

Davy Jones
Davy Jones

Reputation: 673

In my experience, I would suggest DB first, but it may depend on your practical implementation.

DB Frist: Generates models from your database. When you change tables etc. you just update your model.

Code First: Creates your database from your model classes. When updates are needed, you may need to write update scripts which will drop and recreate your database.

Regarding your data access, I can recommend checking out Entity Framework if you are unfamiliar.

Regarding controller templates, you would then choose 2. Read/Write with EF. This will create CRUD methods for your model. If you decide against EF, you may go Empty or Empty Read/Write. All the templates just give you Create/Update/Delete methods that you can change if you wish.

Try it out in a test application - once you see the MVC and EF magic you should feel comfortable to make those decisions.

Upvotes: 1

Related Questions