1. we store the data required by our system under the model. First, we create a database's LINQ to SQL class
2. Select all tables to the design view.
3. Create a bookshopdbdatacontext distribution class. Here we will write some methods to interact with the Controller in the logic layer.
Using System;
Using System. Data;
Using System. configuration;
Using System. LINQ;
Using System. xml. LINQ;
Using System. Collections. Generic;
Namespace Bookshop. Models
{
Public Partial Class Bookshopdbdatacontext
{
// Return all categories
Public List < Category > Getallcategory ()
{
Return Categories. tolist ();
}
// Add Category
Public void addcategory (category C)
{< br> This . categories. insertonsubmit (c);
This . submitchanges ();
}
// edit Category
Public void editcategory (category C)
{< br> This . updatecategory (c);
This . submitchanges ();
}
Public category getcategory ( int ID)
{< br> return categories. single (C => C. categoryid = ID );
}
//Delete category
Public BoolDelcategory (IntID)
{
// Check if has used
If (Books. Any (B => B. bookcategoryid = ID ))
{
Return False ;
}
Else
{
CATEGORY delcategory = This . Categories. Single (C => C. categoryid = ID );
This . Categories. deleteonsubmit (delcategory );
This . Submitchanges ();
Return True ;
}
}
}
}