Configuring Entity Framework and Identity services

We can add the Entity Framework and Identity as middleware. ConfigureServices is the entry point where all services are added and called by runtime.

Add the following code snippet in the ConfigureServices method to add Entity Framework:

    services.AddDbContext<ApplicationDbContext>(options => 
options.UseSqlServer(Configuration.GetConnectionString(
"DefaultConnection")));

Next, we can add Identity, as follows:

    services.AddEntityFrameworkSqlServer() 
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
.AddDefaultTokenProviders();

To add the SQL Server support, we have to call the AddEntityFrameworkSqlServer method and then we specify the DbContext, which is ApplicationDbContext, by calling the AddDbContext method.

The following code will add the Identity framework into your application:

    services.AddIdentity<ApplicationUser, IdentityRole>() 
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

AddIdentity also takes options to configure identity options. There are two built-in options provided, known as AllowedUserNameCharacters and RequireUniqueEmail.

  • AllowedUserNameCharacters: To accept characters in a username
  • RequireUniqueEmail: E-mail should be unique
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.46.78