In this session, we are going to implement CRUD operations in ASP.NET Core MVC Web app, also we upgrade our web app .NET framework to use the latest released version. During this session, we complete the Cost model Interface and repository to handle CRUD operations. CRUD stands for Create, Read, Update and Delete operations that are four basic operations of storage which could be memory or database. Thus, for creating, reading, updating, and deleting data from storage we need a repository that handles all the mentioned verbs.
What is CRUD operations?
Generally, in programming and database, CRUD stands for Create, Read, Update and Delete are four basic operations that should be implemented in each persistent storage application. On the other hand, each persistent storage application should have the ability to create a new record, read, update and delete the existed data.
Implement Repository to Hande CRUD Operations in ASP.NET Core MVC
During sessions fifteen and sixteen, we discussed in detail about Interface, Repository, and Dependency Injection. In this session, we work again with the Cost Repository to cover all CRUD functions. Thus, first, we need to add the required Methods to ICostRepositoty.
1 2 3 4 5 6 7 8 9 10 11 12 |
public interface ICostRepository { Cost GetCostByID(int id); IEnumerable<Cost> GetAllCost(); Cost Create(Cost NewCost); Cost Update(Cost UpdateCost); Cost Delete(int id); } |
As you can see, we added methods that perform Create, Read, Update and Delete functions in ICostRepository.
Next, We need to implement these methods in the Repositories. Now we have just StaticCostRepository which works with Static Data.
First, create a new record.
1 2 3 4 5 6 7 8 9 |
public Cost Create(Cost NewCost) { costs.Add(NewCost); return NewCost; } |
Second, Read all the Cost records and a record with a specific ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public IEnumerable<Cost> GetAllCost() { return costs; } public Cost GetCostByID(int id) { return costs.FirstOrDefault(c => c.ID == id); } |
Third, update a specific record.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public Cost Update(Cost UpdateCost) { Cost SelectedCost = costs.FirstOrDefault(x => x.ID == UpdateCost.ID); if (SelectedCost != null) { SelectedCost.Amount = UpdateCost.Amount; SelectedCost.RegisteredDate = UpdateCost.RegisteredDate; SelectedCost.CategoryID = UpdateCost.CategoryID; SelectedCost.Comment = UpdateCost.Comment; SelectedCost.PaymentMethod = UpdateCost.PaymentMethod; return UpdateCost; } return null; } |
Finally, delete a record with a specific ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public Cost Delete(int id) { Cost SelectedCost = costs.FirstOrDefault(x => x.ID == id); if(SelectedCost!= null) { costs.Remove(SelectedCost); return SelectedCost; } return null; } |
Also, you can check all the code which is related to StaticCostRepository which handles CRUD operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public class StaticCostRepository : ICostRepository { private List<Cost> costs = new List<Cost> { new Cost{ID=1, Amount=100.00M,RegisteredDate=new DateTime(2022,02,19),CategoryID=1,Comment="New Record",PaymentMethod=PaymentMethods.Cash }, new Cost{ID=2, Amount=25.00M,RegisteredDate=new DateTime(2022,02,13),CategoryID=2,Comment="New Record",PaymentMethod=PaymentMethods.Credit }, new Cost{ID=3, Amount=700.00M,RegisteredDate=new DateTime(2022,02,11),CategoryID=3,Comment="New Record",PaymentMethod=PaymentMethods.Debit } }; public Cost Create(Cost NewCost) { costs.Add(NewCost); return NewCost; } public Cost Delete(int id) { Cost SelectedCost = costs.FirstOrDefault(x => x.ID == id); if(SelectedCost!= null) { costs.Remove(SelectedCost); return SelectedCost; } return null; } public IEnumerable<Cost> GetAllCost() { return costs; } public Cost GetCostByID(int id) { return costs.FirstOrDefault(c => c.ID == id); } public Cost Update(Cost UpdateCost) { Cost SelectedCost = costs.FirstOrDefault(x => x.ID == UpdateCost.ID); if (SelectedCost != null) { SelectedCost.Amount = UpdateCost.Amount; SelectedCost.RegisteredDate = UpdateCost.RegisteredDate; SelectedCost.CategoryID = UpdateCost.CategoryID; SelectedCost.Comment = UpdateCost.Comment; SelectedCost.PaymentMethod = UpdateCost.PaymentMethod; return UpdateCost; } return null; } } |
Create Repository to Handle CRUD Operations in SQL Server
For creating this repository, first, we need to add the Cost model as a DbSet property to our DbContext class.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class WebAppDBContext : DbContext { public WebAppDBContext(DbContextOptions<WebAppDBContext> options) : base(options) { } public DbSet<Category> Categories { get; set; } public DbSet<Cost> Costs { get; set; } } |
Then, we need to add a Class inside the Models folder which should be implemented from ICostRepository. Also, we should inject DbConetxt class to this Repository with Constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class SQLServerCostRepository : ICostRepository { private readonly WebAppDBContext context; public SQLServerCostRepository(WebAppDBContext _context) { context = _context; } } |
Thereupon, we should implement all the methods that we declared in ICostRepository.
Create a new record in SQL Server Database.
1 2 3 4 5 6 7 8 9 10 |
public Cost Create(Cost NewCost) { context.Costs.Add(NewCost); context.SaveChanges(); return NewCost; } |
Read all the Cost records and a specific Cost record from SQL Server DB.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public IEnumerable<Cost> GetAllCost() { return context.Costs; } public Cost GetCostByID(int id) { return context.Costs.FirstOrDefault(x => x.ID == id); } |
We can implement the update function with two methods.
Method 1: Finding the record and update the fields that we need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public Cost Update(Cost UpdateCost) { Cost SelectedCost = context.Costs.FirstOrDefault(x => x.ID == UpdateCose.ID); if (SelectedCost != null) { SelectedCost.Amount = UpdateCose.Amount; SelectedCost.RegisteredDate = UpdateCose.RegisteredDate; SelectedCost.CategoryID = UpdateCose.CategoryID; SelectedCost.Comment = UpdateCose.Comment; SelectedCost.PaymentMethod = UpdateCose.PaymentMethod; context.SaveChanges(); return UpdateCose; } return null; } |
Method 2: Attaching the updated data and changing the data state.
1 2 3 4 5 6 7 8 9 10 11 |
public Cost Update(Cost UpdateCost) { var SelectedCost = context.Costs.Attach(UpdateCost); SelectedCost.State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return UpdateCost; } |
Also, you can review all the SQLServerCostRepository below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class SQLServerCostRepository : ICostRepository { private readonly WebAppDBContext context; public SQLServerCostRepository(WebAppDBContext _context) { context = _context; } public Cost Create(Cost NewCost) { context.Costs.Add(NewCost); context.SaveChanges(); return NewCost; } public Cost Delete(int id) { Cost SelectedCost = context.Costs.FirstOrDefault(x => x.ID == id); if (SelectedCost != null) { context.Costs.Remove(SelectedCost); context.SaveChanges(); return SelectedCost; } return null; } public IEnumerable<Cost> GetAllCost() { return context.Costs; } public Cost GetCostByID(int id) { return context.Costs.FirstOrDefault(x => x.ID == id); } public Cost Update(Cost UpdateCost) { var SelectedCost = context.Costs.Attach(UpdateCost); SelectedCost.State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return UpdateCost; } } |
Switching Between Repositories
As we described this topic in previous sessions, we can switch between the repositories by changing the active repository in Startup Class. Having said that, we can easily switch between them because of the Dependency Injection that we implemented in our project.
1 2 3 4 5 6 7 8 9 10 11 |
public void ConfigureServices(IServiceCollection services) { services.AddDbContextPool<WebAppDBContext> (options=> options.UseSqlServer(Configuration.GetConnectionString("CostDBConnectionSQLServer"))); services.AddControllersWithViews(); services.AddScoped<ICostRepository, SQLServerCostRepository>(); } |
Upgrade .NET Framework
For upgrading .NET Framework, we need to meet its requirement. For instance, for upgrading the .NET framework of an ASP.NET MVC Web app from version 5 to version 6 you can check this LINK. Also, the requirements are different based on OS and IDE versions.


As you can see in the above figure, for upgrading the Web App .NET framework in different situations, we need to update and install some tools and features. Since we use Microsoft Windows and OS and Visual Studio as ID, we need to set up Visual Studio to upgrade our Web App .NET framework from 5.0 to 6.0.
Then, we can open our project with Visual Studio 2022 and change the Target Framework to version 6.0 from Application-General in the application properties.


Also for more compatibility, It is recommended to upgrade all NuGet Packages to the version which is compatible with the target framework.
If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.
You can download this Session Slides form HERE.