In this tutorial,  I create a project in ASP.net CRUD Web Application using Entity Framework. This Project user can Create, Read(Display), Update, Delete data from Database using Entity Framework. This article will show step by step process. Lets Go.

I. What is ASP.NET?

.NET is a developer platform made up of tools, programming languages, and libraries for building many different types of applications.

The base platform provides components that apply to all different types of apps. Additional frameworks, such as ASP.NET, extend .NET with components for building specific types of apps.

Here are some things included in the .NET platform:

  • The C#, F#, and Visual Basic programming languages
  • Base libraries for working with strings, dates, files/IO, and more
  • Editors and tools for Windows, Linux, macOS, and Docker

II. CRUD Stands For?

CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.

  • Create – to do something new, or save new data, stored new data into database.
  • Read – Read or Display data to deferent from of UI(User Interface or front view) or the system where from database.
  • Update – update data from database, to update specific data from database but something bulk request.
  • Delete – delete or remove data from database.

III. What is Entity Framework?

Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code compared with traditional applications.

Entity Framework is an open source object–relational mapping framework for ADO.NET. It was originally shipped as an integral part of .NET Framework. Starting with Entity Framework version 6, it has been delivered separately from the .NET Framework.

IV. Requirements

Before we start, Please make sure to have installed the following

  • The latest version of Visual Studio
  • SQL Server

Note:

In this tutorial I don’t include who to create database and also table. you can refer to previous blog or topic or just click here to see it.

V. Create ASP.Net Project

  • first open Visual studio, and for this tutorial I’ll use Visual Studio 2019. the latest one. then click Create a new project.
  • After this is the view, select the ASP.NET Web Application (.Net Framework) or refer the screenshot bellow. and the language that we are choosing is C#. then click NEXT.
  • Now we need to configure the setting to proceed in creating ASP.Net Web Application project.
    • Project Name, here I will name the project as EFAppASPNETCRUD because this is Entity Frameword application in ASP.net and the main function is the CRUD.
    • Location, Select where do you save the solution or path of the project file.
    • Solution name, for this project I will name it as same as project name. but you can name your solution what even you want.
    • .NET Framework, I choose 4.7.2 because this is the last stable release of Microsoft, and run perfectly.
    • then Click Create button.
  • In this part I will select MVC, and set no authentication because we don’t need that, because our project is only CRUD. Select default MVC and uncheck the HTTPS Configuration.
  • hit Create Button.
  • here is our project file.

VI. Install Entity Framework Packages

  • Right click the project solution and click Manage NuGet packages.
  • look for EntityFramework, and Click arrow down to donwload the packages or beside that have and another window that display the information or packages. and as you can see has button Install, hit it and packages will install to the project..
  • if there is an confirmation just click accept.
  • after install the packages, as you can see here in the project. there is a Entity Framework libraries available inside the application references.

VII. Configure ADO .NET Entity Framework Connection

  • Right click model folder, click add button and New Item.
  • Click Data in left side of the window. and select ADO .NET Entity Data Model.
  • I will name this as UserCrudDataModel, and click Add button.
  • Select EF Designer from Database and click Next Button.
  • Click New Connection
  • Select Data source as MS SQL Server or Microsoft SQL Server (sqlClient)
  • Input the Server Name. I am using my local machine and that is the Server name of my local machine.
  • Select Database Name, Perfect because in our previous blog/content that is what I create and I will reuse that database name.
  • Click the Test connection, if prompt Test connection is succeeded meaning configured successfully.
  • then click OK to proceed.
  • to proceed click next.
  • and now you can see Tables and select the users table, then Hit Finish button.
  • As you can see. inside the models folder there is a Data Model.

VIII. Create Controller

  • Right click the Controller folder and click add button and also the controller.
  • I will select MVC 5 Controller – Empty and hit Add button.
  • I will name this as UserCrudController.
  • after hitting add button. Controller will be shown in controller folder.

IX. Controller and View per method.

  • Method: Users
  • Controller
 public ActionResult users()
        {
            List<user> lm = new List<user>();
            var con = new DB_Table_TutorialEntities();
            lm = con.users.ToList();
            return View(lm);
        }
  • View
@model IEnumerable<EFAppASPNETCRUD.Models.user>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.password)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TDT)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UDT)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TDT)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UDT)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
            @Html.ActionLink("Details", "Details", new { id=item.id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.id })
        </td>
    </tr>
}

</table>


  • Create Method
 public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(user model)
        {
            try
            {
                var con = new DB_Table_TutorialEntities();
                user m = new user
                {
                    username = model.username,
                    password = model.password,
                    name = model.name,
                    age = model.age,
                    email = model.email,
                    TDT = DateTime.Now,
                    UDT = DateTime.Now
                };
                con.users.Add(m);
                con.SaveChanges();
            }
            catch (System.Exception ex)
            {
                ViewBag.response = ex.Message;
               // throw;
            }
            return RedirectToAction("users", "UserCrud");
        }
@model EFAppASPNETCRUD.Models.user


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>user</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

  • Edit Method
[HttpGet]
        public ActionResult Edit(int id)
        {
            var con = new DB_Table_TutorialEntities();
            user m = con.users.FirstOrDefault(s => s.id == id);
            return View(m);
        }

        public ActionResult Edit(user model)
        {
            try
            {
                var con = new DB_Table_TutorialEntities();
                user m = con.users.FirstOrDefault(s => s.id == model.id);
                m.username = model.username;
                m.password = model.password;
                m.name = model.name;
                m.age = model.age;
                m.email = model.email;
                m.UDT = DateTime.Now;
                con.SaveChanges();
            }
            catch (Exception ex)
            {
                ViewBag.response = ex.Message;
                //throw;
            }
            return RedirectToAction("users", "UserCrud");
        }
@model EFAppASPNETCRUD.Models.user


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>user</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.id)

        <div class="form-group">
            @Html.LabelFor(model => model.username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

  • Delete Method
        public ActionResult Delete(int id)
        {
            var con = new DB_Table_TutorialEntities();
            user m = con.users.FirstOrDefault(s => s.id == id);
            con.users.Remove(m);
            con.SaveChanges();
            return RedirectToAction("users", "UserCrud");
        }
  • Details Method
  public ActionResult Details(int id)
        {
            var con = new DB_Table_TutorialEntities();
            user m = con.users.FirstOrDefault(s => s.id == id);
            return View(m);
        }
@model EFAppASPNETCRUD.Models.user

<div>
    <h4>user</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.username)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.username)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.password)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.password)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.age)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.age)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.email)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.email)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.TDT)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.TDT)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.UDT)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.UDT)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.id }) |
    @Html.ActionLink("Back to List", "Index")
</p>

Related Topics

Leave a Reply

Your email address will not be published. Required fields are marked *