Showing posts with label Entity Framework Core. Show all posts
Showing posts with label Entity Framework Core. Show all posts

Sunday, December 6, 2020

CRUD Operation In ASP. Net Core Web API with Entity Framework Core

This article will explain how to perform CRUD (Create, Read, Update, Delete) operations in Asp.Net Core Web API using Entity Framework Core. We will see step by step instructions about CRUD operations in Asp.Net Core Web API. In this demonstration, we will use the Database First Approach where our database will be ready before creating an actual code.

Create the Database and Tables
Create Asp.Net Core Web API Project
Install Nuget Packages for Entity Framework
Generates Model Classes
Setup Dependency Injection
Create Repository and implement CRUD operations
Create a Controller and Create API call
Test the API End Points in Postman
Enable CORS


Steps - 1 Create Database 
   
tables 

Step - 2 Project - asp.net core 

  setup connection string and install some dll from nuget package 
   
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.SqlServer

after adding this 






Sunday, August 2, 2020

Recent questions asking in interview - C# , asp.net , asp.net mvc , asp.net core , sql , entity framework

 Q. Sql Injection 
 Q. Sql cache 
 Q. Exception Filter purpose
Q. MVC Validations - Jquery ka use yaha par 
Q. Await Async




1. What is the difference between SingleOrDefault and FirstOrDefault?

LINQ provides element operators that return a single element or a specific element from a collection. The elements operators are Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault.

Single

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.
SingleOrDefault

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.

First

It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.

FirstOrDefault

It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.

When to use Single, SingleOrDefault, First and FirstOrDefault

You should take care of following points while choosing Single, SingleOrDefault, First and 
FirstOrDefault:
When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
When you want a default value is returned if the result set contains no record, use SingleOrDefault.
When you always want one record no matter what the result set contains, use First or FirstOrDefault.
When you want a default value if the result set contains no record, use FirstOrDefault.

Perfomance of SingleOrDefault and FirstOrDefault

FirstOrDefault usually performs faster as compared to SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterates the whole collection to find one single match.

Q.2 AllowAnonymous Attribut and Authorize Attribute.?

One of the new features in ASP.NET MVC 4 is the AllowAnonymous Attribute that helps you secure an entire ASP.NET MVC 4 Website or Controller while providing a convenient means of allowing anonymous users access to certain controller actions, like the login and register Actions.


AllowAnonymous Attribute in ASP.NET MVC 4

As mentioned, if you create a new ASP.NET MVC 4 Internet Project in Visual Studio 2010 or Visual Studio 11 and view the AccountController you will notice the generous use of the AllowAnonymous Attribute on various login and register controller actions. Here are a few of those controller actions.

[Authorize] 
public class AccountController : Controller 
{ 
 [AllowAnonymous] 
 public ActionResult Login() 
{ 
 // ... 
 } 
 [AllowAnonymous]
 [HttpPost] 
 public JsonResult JsonLogin(LoginModel model, string returnUrl) 
{ 
 // ...
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Login(LoginModel model, string returnUrl) 
{ 
 // ... 
 }
 [AllowAnonymous] public ActionResult Register() 
{ 
 // ... 
 } 
 [AllowAnonymous] 
 [HttpPost] 
 public ActionResult JsonRegister(RegisterModel model)
 {
 // ... 
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Register(RegisterModel model) 
{ 
 // ... 
 } 
}

The idea is pretty simple. The Authorize Attribute on the AccountController in this ASP.NET MVC 4 Application denies anonymous access to every controller action. However, we need to allow anonymous access to the login and register controller actions so we decorate them with the AllowAnonymous Attribute which negates the Authorize Attribute and allows anonymous access.

Q.3 What is mvc Filters and custom filters . ?


ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.

Q.4 What is Dependency injection with examples . ?

In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical "using" relationship the receiving object is called a client and the passed object is called a service.


Wednesday, July 29, 2020

ASP.NET Core – CRUD Using Angular And Entity Framework Core



Step - 1: Overview
In this article, I am going to create a web application using ASP.NET Core 3.1 and Angular 8.0 with the help of Entity Framework Core database first approach. I will be creating a sample Employee Record Management system. To read the inputs from the user, we are using Angular Forms with required field validations on the client-side. We are also going to bind a dropdown list in the Angular Form to a table in the database using EF Core. We will also learn how to deploy this application on IIS.

Step - 2 Required Tools
Install .NET Core 3.1 or above SDK from here.
Install the latest version of Visual Studio 2019 Community Edition from here.
Download and install the latest version of Node.js from here.
SQL Server 2008 or above.

We will be using Visual Studio 2017 and SQL Server 2012 or other like 2014, 2016, etc.

Step - 3 Database
We will be using two tables to store our data.

Employee: – Used to store the details of the employee. It contains fields such as EmployeeID, Name, City, Department, and Gender.
Cities: – This contains the list of cities and used to populate the City field of the Employee table. It contains two fields CityID and CityName.


CREATE TABLE Employee (
EmployeeID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Name varchar(20) NOT NULL ,
City varchar(20) NOT NULL ,
Department varchar(20) NOT NULL ,
Gender varchar(6) NOT NULL
)
GO
CREATE TABLE Cities (
CityID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
CityName varchar(20) NOT NULL
)
GO
STEP - 4 Create MVC Web Application
Open Visual Studio and select File >> New >> Project.
After selecting the project, a “New Project” dialog will open. Select .NET Core inside the Visual C# menu
Then, select “ASP.NET Core 3.1 Web Application” from available project types. Put the name of the project as Proapp and press OK.e Model to the Application
We are using Entity Framework core database first approach to create our models. Navigate to Tools >> NuGet Package Manager >> Package Manager Console.
We have to install the package for the database provider that we are targeting which is SQL Server in this case. Hence run the following command:

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Scaffold-DbContext "Your connection string here" Microsoft.EntityFrameworkCore.SqlServer -
OutputDir Models -Tables tblEmployee, tblCities








ASP.NET Core 3.1 - JWT Authentication Tutorial with Example API (JSON Web Token (JWT))

JSON Web Token (JWT) is a means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is digitally signed using JSON Web Signature (JWS) and/or encrypted using JSON Web Encryption (JWE).


This Tutorial we are going to build with Asp.net Core 3.1


In this tutorial, we'll go through a simple example of how to implement custom JWT (JSON Web Token) authentication in an ASP.NET Core 3.1 API with C#.


Tools required to run the ASP.NET Core 3.1 JWT Example Locally


Rewrite whole Web Site System and Sub System with data migration to new system.

Can you please share me your skype id or whats up number for better communications  my skype id is - jitendra.tech  whats up - +919617741414...