Posts

Scaffolding DbContext (Reverse Engineering) | Database first approach.

Image
  With scaffolding, we can import a database schema into code as entity type classes. This process is called reverse engineering.  In a migration, we create entity type classes first, then configure them in the DbContext class. After that, we run the migration command to create a migration file. Finally, with the Update-Database command, we can migrate our code into the database. Command of Entity framework using package manager console : Scaffold-DbContext Dot net Cli (vscode) dotnet ef dbcontext scaffold The above command can be used to Scaffold database schema into entity type classes. Let's test the above process. First, we need to create a schema in our database. Then, we need to establish a connection to the database using a connection string in our code. Let's create an Order Table in our database. Connect database: We are using SSMS so we have two ways to write schema either using sql command or interface of SSMS  Create Table using SSMS interface  Add Auto...

Building Your Data Layer: Creating Entities, Migrations, and Adding References in .NET Core

Image
  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...