Introduction
Hello Folks,
In this article, I am going to show you guys how to use ADO.NET and Entity Framework Core in .NET Core MVC. This way you can use either both or one way to implement your application as per your requirements. I attached the project solution which you can refer to. Also, I have shown a repository pattern to complete this project.
Step 1. I have used an MS SQL server for the database.
Step 2. Change in the startup.cs file in the project root.
![Project root]()
Step 3. Connection string in appsettings.json.
![Logging]()
Step 4. Made a model to communicate with View and DB Layer.
![Testdemo]()
Step 5. Controller class CrudController.cs with dependency injection use with helps to lose coupling while switching from ADO.NET to Entity framework and vice-versa.
![Controller]()
Step 6. The project consists of class files for the DB layer and Repository folder and one partial view for showing the detail view.
- We have created an interface in the repository folder to make a service and use both classes as a service.
- We have created the DBLayer folder and created 2 classes one consists of ado.net insert and select the method and another one for Entity framework Core DBContext.
- In Partial View, I have injected the service directly so that you do not need to pass the model to communicate with a partial view. you can check the getdata() method by debugging and uncommenting the specific section over there.
- System.Data. SQL client reference is required to use ado.net, you can add this by using the NuGet package manager.
DataAccessDB.cs
DBAccessContext.cs
CrudRepository.cs
CrudContextRepository.cs
ICrudRepository.cs
Index. cshtml under View/Crud/
![DBLayer]()
I hope it helps. If you have any questions or discussion remarks, please comment below. Also, I strongly recommend to download the project file and debug the process.