Building Your Data Layer: Creating Entities, Migrations, and Adding References in .NET Core
We will learn how we can add a new entity in a .net project and update it into a database using migration.
In this we are going to create product entity
Before creating an entity we can create a product controller which is not necessary to learn to add a new entity but we can learn adding full api for Product entity.
Code example
Other methods will be created in a similar way shown in above image.
Lets Create an entity now
We are going to keep everything into the entity folder, for a better structure we can add a DAL layer and add our code there.
We will also learn about Repository patterns so we can access common methods of databases from that.
After move our DbContext to database layer, it start showing references errors, lets resolve it by adding references(Dependencies) to DAL layer
Right click on dependencies, it will show options
There are multiple ways to add references in the project
1 option will add reference to a project. If we choose this we can add an ESTORE project into that and that project contains all the necessary dependencies that need to be added to the DAL layer. Lets try this option
Now all there error resolve in the DbContext file
Do the same for Migration Folder , let's move it to DAL layer from ESTORE
All errors are resolved now, because DAL has ESTOREContext (DbContext class)
Now we have some reference issues in the Program.cs file because it is looking for DbContext Reference which does not exist in the ESTORE project, now we can do one thing which is Add Reference of DAL to ESTORE but it will create circular dependencies. Because ESTORE is already added in DAL. So better is to isolate DAL from ESTORE, we can remove ESTORE from DAL and Add required dependencies to DAL layer.
When I try to add DAL in the ESTORE Project it does not add it.
Lets remove ESTORE from DAL
Now add DAL project into ESTORE
Now its allow to add this DAL project in ESTORE, lets add required dependencies in DAL for DbContext(Identity)
We can search package on nuget website
After a package is found click on it to see commands to install
This is dotnet command (vscode) . We can use package manager or install dependencies using visual studio nuget feature.
We can add these using package reference , double click on project name , then you can see xml file add this line their
So our DAL layer needs these Package References, we already have these in ESTORE so we can copy and paste it in DAL project xml file
DAL xml files after adding References
So after adding these Packages References, Build the Project
No more Reference Errors there now
Now Resolve Program.cs REF errors
Click on the error word so the mouse cursor will be moved there after that press CTRL + DOT (ctrl .) cmd it will show solution to fix errors, just like image we have add the name (ref) of the file
All errors fixed
Let's build the project and run the project to see its working
Its working fine without any issue, so we are learned how to Add Layer and how to fix References errors in PROJECT, also we should not try to add Circular dependencies
It is call CODE FIRST APPROACH
Create Product class in DAL’s Entities folder.
We included Data annotation with properties of class.
Next Step is Register this Product entity class in DbContext Class
Error
Fix: make it public
Now let's create a Migration for Product table.
Open PMC
Code generated by Migration CMD
Now lets update our database
Product Table Updated successfully , Let's confirm it on SSMS
It's added.
So this is how we can add tables using CODE first approach. (using migration)
In the DB first approach we first add tables in the database directly and then Scaffold the database in the project using CMD in the package manager console.
Lets Create Repository To access data
The Repository Pattern is a design pattern that provides an abstraction layer between the data access layer and the business logic layer of an application. This pattern helps to isolate data access logic and business logic, making the code more maintainable and testable.
Let's create Base Repository
Add Method signatures in IRepository.cs and Implement this Interface in Repository, these class and interface will treated as base class and it is generic class which accept T type as class
Create generic IRepository Interface and add necessary method’s signatures
Now Create generic Repository Class and Implement IRepository Interface
So we have completed our Repository Service and whenever we want to create an Entity Repository we have to implement this Repository with a newly created Entity Repository. In our case It ProductRepository
To Provide it as Dependency To other class we have to create IProductRepository Interface
Let's Create IProductRepository and Implement IRepository interface
Now create ProductRepository and implement Repository class and IProductRepository interface
Now our ProductRepository is ready to use but we have to register our dependencies in Program.cs file first
Then we can use it.
Lets Register Dependencies
We register both Services(repositories)
They have register in different way
Generic Repository registration is a little bit different than others. We are passing typeof(.. ) as params
So now we can use our ProductRepository. We are going to do this in ProductController. We can create a Business Layer for better architecture and manage our business logic there and can inject Repositories there.
Let's do it with ProductController
Lets test Create product
Let's Test Get Product now
Let's check database
So we tested the Get and Save method , we can include other necessary methods in our controller as well in our Repository.
To get all using edmx linq
Find by where conditions
Add multiple data
To update, remove and remove all
Comments
Post a Comment