Asp.net Mvc Database First Approach Crud

Mushahid AbbasiMushahid Abbasi
3 min read

Create custom with database first approach

ChatGPT said:

ChatGPT

Memory updated

Creating a CRUD application using the Database First approach in ASP.NET MVC 5 involves the following steps:

1. Set Up Your Database

  • Start with an existing SQL Server database or create a new one.

  • Ensure the database contains the tables you want to work with. For example, suppose you have a Books table.

2. Create an ASP.NET MVC 5 Project

  • Open Visual Studio.

  • Create a new project by selecting "ASP.NET Web Application (.NET Framework)".

  • Choose the "MVC" template.

3. Add ADO.NET Entity Data Model

  • Right-click on the "Models" folder in the Solution Explorer.

  • Select "Add" > "New Item".

  • Choose "Data" from the left panel and select "ADO.NET Entity Data Model". Name it, e.g., LibraryModel.

  • Choose "EF Designer from database" in the wizard.

4. Connect to the Database

  • In the "Choose Your Data Connection" step, connect to your database.

  • Select the tables, views, and stored procedures you want to include in the model.

  • The wizard will generate the Entity Framework model based on your database schema.

5. Create the Controller

  • Right-click the "Controllers" folder and select "Add" > "Controller".

  • Choose "MVC 5 Controller with views, using Entity Framework".

  • Select the model you want to use (e.g., Book).

  • Choose the data context class that was automatically generated (e.g., LibraryEntities).

6. Generate Views

  • Visual Studio will generate a controller with actions for Create, Read, Update, and Delete (CRUD) operations.

  • It will also generate the corresponding views (e.g., Create, Edit, Details, Delete, and Index).

7. Customize the Controller and Views

  • Modify the generated controller actions and views as needed.

  • For example, you can add data validation, customize the layout, or add business logic.

Example of a custom controller method:

csharpCopy codepublic ActionResult Search(string query)
{
    var books = db.Books.Where(b => b.Title.Contains(query)).ToList();
    return View("Index", books);
}

Example of a custom view:

htmlCopy code@model IEnumerable<YourNamespace.Models.Book>

<h2>Book List</h2>

<form method="get" action="@Url.Action("Search")">
    <input type="text" name="query" placeholder="Search by title..." />
    <input type="submit" value="Search" />
</form>

<table>
    <tr>
        <th>Title</th>
        <th>Author</th>
        <th>Price</th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Title)</td>
            <td>@Html.DisplayFor(modelItem => item.Author)</td>
            <td>@Html.DisplayFor(modelItem => item.Price)</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>

8. Run the Application

  • Press F5 to build and run your application.

  • You should now have a fully functional CRUD application with data being retrieved and managed directly from your database.

9. Deploy the Application (Optional)

  • Once development is complete, you can deploy your application to a web server or cloud platform.

This process leverages the existing database schema and allows for rapid development by generating much of the necessary code automatically, which you can then customize according to your needs.

0
Subscribe to my newsletter

Read articles from Mushahid Abbasi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mushahid Abbasi
Mushahid Abbasi