How to create Linq to SQL Class
Part1: start MVC application
First Create Database Name and tables
Note: Database Details
Servre name is lokesh
Create Database Purchase
Create table tblCustomers
CREATE TABLE tblCustomer(
[CustomerID] [int] NOT NULL,
[Name] [nvarchar](32) NOT NULL,
[Address] [nvarchar](64) NOT NULL,
[EmailID] [nvarchar](32) NOT NULL,
[PhoneNo] [nvarchar](16) NOT NULL,
[State] [nvarchar](32) NOT NULL,
[Country] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_tblCustomer] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Create Linq to SQL Class
Step 1: In Visual Studio application -> Go to Project Menu ->Website -> Add New Item
Note: In MVC Application Right Click in Model folder in Solution Explorer then click Add New Item option
Step 2: Choose Data from Add New Item Dialog Box (Categories Visual C#) -> choose LINQ to SQL Classes.
See below figure.
Linq-to-sql-class
In Add New Item Dialog Box below you will see Name of the Linq to SQL Classes.
By Default, DataClasses1.dbml in this application change the name of DataClasses1.dbml to Purchase.dbml then click Add button.
Purchase.dbml
Step3: After clicking Add button in Add new Item dialog box you will see the below
Figure with name Purchase.dbml (top like Default.aspx page)
Note: In MVC, Model Folder inside Purchase.dbml file creates
Step4: Click in Server Explorer (Goto Menu bar –> View –> Solution Explorer) then Server Explorer popup see below figure
Note: Server Exploer can view in Go to View Menu –> choose Server Explorer
Solution Explorer
Step5: Go to Data Connections -> Choose Add Connection… -> Then Add Connection Dialog box Popup
- Choose Server Name in Dropdown (Ex: lokesh)
- Choose Yours Authentication depends upon your Sql Server
Use Windows Authentication
User SQL Server Authentication
(In this application SQL Server Authentication using)
- Select or enter A Database Name from Dropdown you can select database
linq-add-connection
Note: Check Test Connection if everything is correct then Test Connection succeeded displays
Then click OK.
Step6: In Server Explorer, Data Connections will adds Lokesh.Purchase.dbo
serverExplorerDBchoose
Then Click in lokesh.Purchase.db0 +sysmbol will expand -> choose Tables expand ->
Drag tblCustomer table into Purchase.dbml page
(which is present in Model folder[Purchase.dbml] in MVC Application)
Purchase.dbml
This is the steps to Create LINQ to SQL Classes.
Don’t Forget after creating LINQ to SQL Classes (Purchase.dbml) Build the application (Build Menu in VS 2005 or VS2008 -> Build Solution)
