Web
Analytics
LINQ CRUD in asp.net MVC using LINQ to SQL - Tutorial

For Consultation : +91 9887575540

Stay Connected :

LINQ CRUD in asp.net MVC using LINQ to SQL

Step1. Create new Empty MVC Project

Open Visual Studio->New Project->Visual C#->Web->ASP.NET Web application->Give Project Name->Select Empty Template->Check MVC Checkbox->Click OK

Step 2. Now to we will add LINQ to sql classes(dbml) in Models folder .

Right click on Model folder =>select new Item->Select Data tab at left side in opened window->Select LINQ to sql classes in middle.->Give the name of your dbml file(here DataClasses1.dbml)




Step 3. Connect with sql database .We have to create sql database in sql server management studio.I supposed you  already created it so I am not telling way to create it.




Open server explorer(Ctrl+alt+s)->Right click on data connection and select add connection->Enter your sql server name->Select Database(my Database name is Ganesha).

(In this process DataClasses1DataContext will be created in models folder)

LINQ_database_Table

 

Create table sql query

 




I want to perform CRUD on this table.

Step 4. In server explorer window expand newly added(ganesha) database->drop Employee Table on design serface.+Ctrl S

(It will add employee model in DataClasses1DataContext)

Step 5. Create controller in controller folder-> Select Create MVC controller with read write action->Controller Name(Here Employee)=>OK

Pass namespace using LINQCRUD.Models

Write following code in Employee Controller

 




Add Views with Scaffolding with respective actions.

It will gererate html markup and it will show employee data.

Note: Using these scaffolding options we can create views for create, details, edit and delete  actions.

Note: Please insert some dummy rows in employee table to see results.

AddIndex_View_using_scaffolding create delete Details Edit

 




Download Source Code

Select your currency