Posts

Showing posts from June, 2022

Web API Using EF Core

Image
What is Web API  As the name suggests, is an API over the web which can be accessed using HTTP protocol. We can build Web API using different technologies such as Java, .NET etc.  Web API breaks the limitation of language. For example a web API written in .Net can be utilized by Java and vice verssa, because the request/response format of Web API is JSON which is understood by all language. For example, Twitter's  REST APIs  provide programmatic access to read and write data using which we can integrate twitter's capabilities into our own application. We can use tools like Fiddler, PostMan and Swagger for testing Web API .Net Core Web API -   Is an Ideal platform for building RESTful services. Is built on top of ASP.NET and supports ASP.NET request/response pipeline Maps HTTP verbs to method names. Supports different formats of response data. Built-in support for JSON, XML, BSON format. C...

Entity Framework DB First

  Create a Project of Type Class Library with the name EFDBFirst.Data Add following Nuget Packages Microsoft.EntityFrameworkCore (6.0.6 or latest stable package) Microsoft.EntityFrameworkCore.SQLServer (6.0.6 or latest stable package) Microsoft.EntityFrameworkCore.Tools (6.0.6 or latest stable package) Microsoft.EntityFrameworkCore.Design(6.0.6 or latest stable package) Create a New Database with the name DBFirst and Add 2 tables Employee and EmployeeEducation with 1 to many relationship Run the following commands in package manager console Scaffold-DbContext "Server=KELLGGNLPTP1078\SQLEXPRESS;Database=DBFirst;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models This command will generate the DB context class and model for all table. After this the process remains same as code First Approach If we make any changes in DB tables and wanted to update our code then we need to run Scaffold-DbContext command with -force flag Scaffold-DbContext "S...

Entity Relationship Using EF Core Code First Approach

 In this section, we are going to talk about the different kind of relationship between entities and how to establish that using C# code. Followings are the type of relationship One to Many An employee can have more than 1 education details. To Achieve this, we need to have a property in employee class like the below one. public ICollection<EmployeeEducation> EducationList { get; set; } Once we do this and add the migration, it will add a new column EmployeeID in EmployeeEducations table, but as a nullable attribute. In this case, if we try to add data in EmployeeEducations table, it will set EmployeeID as null To fix this, we need to add a cross reference in the EmployeeEducation Class, to denote that Every education record must have an employee ID. This will ensure that allow null attribute of EmployeeID column in EmployeeEducations  table is set to false. public Employee Employee { get; set; } If we try to insert values in EmployeeEducations table with below code...

CRUD Operation with EF Core Using Class Library and Console Application

Image
What is Entity Framework Core Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables .NET developers to work with a database using .NET objects. Eliminates the need for most of the data-access code that typically needs to be written. We can either use Code First or DB First Approach to work Pros of Entity Framework Code First You can create a database and required tables from business entities It is recommended for small applications that does not involve extensive data processing You can specify the collections for eager loading and the serialization of data It provides full access over the code and you can do modifications easily in the code Cons of Entity Framework Code First You need to write the code related to the creation of database If there is any change in database after the creation, you need to do it in ...