CRUD Operations in ASP.Net Using Entity Framework

For more detail about Entity Framework go to EntityFramework Tutorials.

1. First go to SQL Server and execute following query for Student Department and Student Information.
  1. Create table stdDepartment  
  2. (  
  3.    Id int primary key identity,  
  4.    Name nvarchar(50)  
  5. )  
  6. Insert into stdDepartment values('Teacher')  
  7. Insert into stdDepartment values('HOD')  
  8. Insert into stdDepartment values('Professor')  
  9. Create Table Student  
  10. (  
  11.    StudentID int identity primary key,  
  12.    StudentName nvarchar(50),  
  13.    Gender nvarchar(50),  
  14.    SchoolName nvarchar(50)  
  15. )  
  16. Alter table Student add DepartmentId int  
  17. Alter table Student  
  18. add foreign key (DepartmentId)  
  19. references stdDepartment(Id)  
  20. Insert into Student values ('Munesh','Male','VIT',1)  
  21. Insert into Student values ('Rahul','Male','ABC',2)  
  22. Insert into Student values ('Reet','Female','XYZ',2)  
  23. Insert into Student values ('Anshuman','Male','PQR',3)  
  24. Insert into Student values ('Simi','Female','NIT',3)  
  25. Insert into Student values ('Nency','Female','MIT',2)  
  26. Insert into Student values ('XYZ','Male','PQR',1)  
2. Now install Entity Framework into your application. For that Go here.


3. After installing Entity Framework add an “Ado.Net Entity DataModal” by right-clicking on “Models” and provide the name for this as “StudentDataModal”.
 
 
 
 
 

3.
4. When You click the Add button you will see another window for “Entity Data model Wizard”. From there select “Generate From DataBase”, and click Next.
5. In This window you provide your database connection and select your database:
After clicking the Continue Button the following screen will be shown. There you will specify the database connection and test your connection and click OK.
Then click Next.
6. In this screen select your database tables and provide the Model name then click the FINISH Button.

7. When you click on the Finish button it will create the StudentDepartment and Student Entities, change their name also.
8. Right-click on Controller and add a controller and provide details for the controller as in the following:
Then click on Add.
9. When you click on Add it will create a View automatically. You can see it in the View folder as in the following:
10. At this time, if we run our application, it will produce the error that “Resource cannot be found” because in the RouteConfig file the default controller name is “Home “ controller, so change this controller name to “Student” controller and run the application.
Delete the following script from the “Create.csHtml and “Detail.csHtml” Views.
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
Now run your application and see the output and perform all the operations.
You can find this in my blog_munesh.


Similar Articles