This article describes how to perform basic CRUD operations in an MVC4 application using Entity Framework 5 without writing a single line of code.
IntroductionThis article describes how to perform basic CRUD operations in an MVC4 application using Entity Framework 5 without writing a single line of code. Entity Framework and MVC have advanced to the level that we don't need to put effort into doing extra work.
Step 2: Open your Visual Studio (the Visual Studio Version should be greater than or equal to 12) and add an MVC internet application as in the following:I have given it the name "KnockoutWithMVC4".Step 3: You'll get a fully structured MVC application with default Home controller in the Controller folder. By default Entity Framework is downloaded as a package in the application folder, but if not then you can add the Entity Framework package by right-clicking the project, select "Manage nugget packages" and search and install the Entity Framework.Step 4: Right-click the project file, select "Add a new item" and add the ADO .Net Entity Data Model, follow the procedure in the wizard as shown below:Generate a model from the database, select your server and "LearningKO" database name, the connection string will automatically be added to your Web.Config, name that connection string "LearningKOEntities".Select tables to be added to the model. In our case it's the "Student" table.Step 5: Now add a new controller to the Controller folder, right-click the controller folder and add a controller named "Student". Since we have already created our data model, we can choose for an option where CRUD actions are created by the chosen Entity Framework data model.
Now you can perform all the operations on this list.Since I have not provided any validation checks on the model or created an existing student id, the code may break, so I am calling Edit Action in the create when we find that the id already exists.Now create a new student.We see that the student is created successfully and added to the list.In the database:Similarly for an edit:Change any field and press "Save".The change will be reflected in the list and the database.For delete:Student deleted.And in the database:Not a single line of code has been written.ConclusionIn this tutorial we learned to set up an environment for MVC and Entity Framework 5 and perform CRUD operations on a Student model without writing a single line of code. You can expand the application by adding multiple Controllers, Models and Views.Note: a few of the images in this article were obtained via Google search.
Read more:
My other series of articles:
For more informative articles visit my Blog.
Happy Coding.
Hands on ASP.NET GridView