how to implement crud operations with class, interface along with DB

Murari Ram 21 Reputation points
2022-08-08T13:39:37.82+00:00

how to implement crud operations with class, interface along with SQL server DB. Please guide me on this

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,146 Reputation points
    2022-08-08T14:03:39.177+00:00

    Your question is too open to answer directly. I'm guessing you are asking how to create a layered application using the repository pattern.

    See the following ASP.NET MVC tutorial.

    Getting Started with EF 5 using MVC 4

    Step 9 of 10 covers the repository and unit of work pattern. I think it goes without saying that you should start at step one.

    Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)


  2. Karen Payne MVP 35,196 Reputation points
    2022-08-08T18:44:16.897+00:00

    Why don’t you want to use Entity Framework? Entity Framework makes performing all aspects of data operations easier and with less maintenance and refactoring and is less prone to many issues that can happen.

    Example, editing a record in EF Core

    public static bool EditCustomer(Customer customer)  
    {  
        using CustomerContext context = new();  
        context.Entry(customer).State = EntityState.Modified;  
        return context.SaveChanges() == 1;  
    }  
    

    Now without EF Core

    public static int EditCustomer(Customer customer)  
    {  
        using var cn = new SqlConnection(ConfigurationHelper.ConnectionString());  
        using var cmd = new SqlCommand { Connection = cn, CommandText = SqlStatements1.UpdateCustomer};  
      
        cmd.Parameters.Add("@CompanyName", SqlDbType.NChar).Value = customer.CompanyName;  
        cmd.Parameters.Add("@ContactName", SqlDbType.NChar).Value = customer.ContactName;  
        cmd.Parameters.Add("@ContactTypeIdentifier", SqlDbType.Int).Value = customer.ContactTypeIdentifier;  
        cmd.Parameters.Add("@GenderIdentifier", SqlDbType.Int).Value = customer.GenderIdentifier;  
        cmd.Parameters.Add("@Identifier", SqlDbType.Int).Value = customer.Identifier;  
      
        cn.Open();  
      
        return cmd.ExecuteNonQuery();  
      
    }  
    

    Now imagine you changed a column name in a table, with EF Core we re-reverse engineer or use migrations while without EF Core we must make manual changes. This carries through with all operations, read, edit, remove, find, add etc.

    One more EF Core example, find by multiple primary keys, not that easy without EF Core

    public static async Task<Customer[]> CustomersWhereInAsync(int[] primaryKeys)  
    {  
        await using CustomerContext context = new();  
        return await context.FindAllAsync<Customer>(primaryKeys.ToObjectArray());  
    }  
    
    0 comments No comments