Mvc crud operation without entity framework

MVC CRUD operation without Entity Framework

In MVC (Model-View-Controller) architecture, CRUD operations refer to Create, Read, Update, and Delete operations on data. While Entity Framework is commonly used for database operations in MVC applications, it is still possible to perform CRUD operations without using Entity Framework. Below, we’ll discuss each operation and provide examples without using Entity Framework.

Create Operation

To create data without Entity Framework, you can perform the following steps:

  1. Create a model class that represents the data you want to store.
  2. Create a controller method to handle the creation process. In this method, you can manually connect to the database using ADO.NET or any other ORM (Object-Relational Mapping) tool.
  3. In the view, create a form that captures the necessary data from the user.
  4. Submit the form to the controller method and perform the necessary data insertion logic.
  5. Redirect the user to a different view to indicate the successful creation.

Example:


// Model
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Controller
public class ProductController : Controller
{
    [HttpPost]
    public ActionResult Create(Product product)
    {
        // Insert the product into the database using ADO.NET or any other ORM
        // Redirect to a different view upon successful creation
        return RedirectToAction("Index");
    }
}

// View
@using (Html.BeginForm("Create", "Product", FormMethod.Post))
{
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name)
    
    @Html.LabelFor(model => model.Price)
    @Html.TextBoxFor(model => model.Price)
    
    
}
  

Read Operation

To read data without Entity Framework, you can perform the following steps:

  1. Create a controller method to handle the retrieval process. In this method, you can manually connect to the database using ADO.NET or any other ORM tool.
  2. Query the database and retrieve the desired data.
  3. Pass the retrieved data to the view.
  4. In the view, display the data as required.

Example:


// Controller
public class ProductController : Controller
{
    public ActionResult Index()
    {
        List<Product> products = new List<Product>();

        // Retrieve the products from the database using ADO.NET or any other ORM
        // Example using ADO.NET:
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM Products", connection);
            SqlDataReader reader = command.ExecuteReader();
            
            while (reader.Read())
            {
                Product product = new Product();
                product.Id = (int)reader["Id"];
                product.Name = (string)reader["Name"];
                product.Price = (decimal)reader["Price"];
                products.Add(product);
            }
        }

        return View(products);
    }
}

// View
@model List

@foreach (var product in Model)
{
    

@product.Name - $@product.Price

}

Update Operation

To update data without Entity Framework, you can perform the following steps:

  1. Create a controller method to handle the update process. In this method, you can manually connect to the database using ADO.NET or any other ORM tool.
  2. Retrieve the existing data that needs to be updated.
  3. In the view, display the existing data pre-filled in a form, allowing the user to make changes.
  4. Submit the updated form to the controller method and perform the necessary data update logic.
  5. Redirect the user to a different view to indicate the successful update.

Example:


// Controller
public class ProductController : Controller
{
    public ActionResult Edit(int id)
    {
        Product product = new Product();
        
        // Retrieve the product by its ID from the database using ADO.NET or any other ORM
        // Example using ADO.NET:
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM Products WHERE Id = @id", connection);
            command.Parameters.AddWithValue("@id", id);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                product.Id = (int)reader["Id"];
                product.Name = (string)reader["Name"];
                product.Price = (decimal)reader["Price"];
            }
        }

        return View(product);
    }

    [HttpPost]
    public ActionResult Edit(Product product)
    {
        // Perform the necessary update logic using ADO.NET or any other ORM
        // Redirect to a different view upon successful update
        return RedirectToAction("Index");
    }
}

// View
@model Product

@using (Html.BeginForm("Edit", "Product", FormMethod.Post))
{
    @Html.HiddenFor(model => model.Id)
    
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name, new { value = Model.Name })
    
    @Html.LabelFor(model => model.Price)
    @Html.TextBoxFor(model => model.Price, new { value = Model.Price })
    
    
}
  

Delete Operation

To delete data without Entity Framework, you can perform the following steps:

  1. Create a controller method to handle the deletion process. In this method, you can manually connect to the database using ADO.NET or any other ORM tool.
  2. Retrieve the existing data that needs to be deleted.
  3. In the view, display the existing data and provide a button or link to confirm the deletion.
  4. Upon confirmation, submit the deletion request to the controller method and perform the necessary data deletion logic.
  5. Redirect the user to a different view to indicate the successful deletion.

Example:


// Controller
public class ProductController : Controller
{
    public ActionResult Delete(int id)
    {
        Product product = new Product();
        
        // Retrieve the product by its ID from the database using ADO.NET or any other ORM
        // Example using ADO.NET:
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM Products WHERE Id = @id", connection);
            command.Parameters.AddWithValue("@id", id);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                product.Id = (int)reader["Id"];
                product.Name = (string)reader["Name"];
                product.Price = (decimal)reader["Price"];
            }
        }

        return View(product);
    }

    [HttpPost]
    public ActionResult Delete(Product product)
    {
        // Perform the necessary deletion logic using ADO.NET or any other ORM
        // Redirect to a different view upon successful deletion
        return RedirectToAction("Index");
    }
}

// View
@model Product

Are you sure you want to delete this product?

@Model.Name - $@Model.Price

@using (Html.BeginForm("Delete", "Product", FormMethod.Post)) { @Html.HiddenFor(model => model.Id) }

These examples demonstrate how to perform CRUD operations in MVC without using Entity Framework. By manually connecting to the database and utilizing plain ADO.NET or other ORM tools, you can achieve the desired functionality. Keep in mind that these examples are simplified and may require additional error handling and security measures depending on your specific application needs.

Read more

Leave a comment