diff --git a/README.md b/README.md index 5acfc4e2..c62d68ba 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,15 @@ To use OpenIddict, you need to: public void ConfigureServices(IServiceCollection services) { services.AddMvc(); - services.AddDbContext(options => - options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); + services.AddDbContext(options => { + // Configure the context to use Microsoft SQL Server. + options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]); + + // Register the entity sets needed by OpenIddict. + // Note: use the generic overload if you need + // to replace the default OpenIddict entities. + options.UseOpenIddict(); + }); // Register the Identity services. services.AddIdentity() @@ -129,43 +136,30 @@ public void Configure(IApplicationBuilder app) { - **Update your Entity Framework context registration to register the OpenIddict entities**: ```csharp -public class ApplicationDbContext : IdentityDbContext { - public ApplicationDbContext(DbContextOptions options) - : base(options) { } - - protected override void OnModelCreating(ModelBuilder builder) { - // Register the entity sets needed by OpenIddict. - // Note: use the generic overload if you need - // to replace the default OpenIddict entities. - builder.UseOpenIddict(); - - base.OnModelCreating(builder); +services.AddDbContext(options => { + // Configure the context to use Microsoft SQL Server. + options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]); - // Customize the ASP.NET Identity model and override the defaults if needed. - // For example, you can rename the ASP.NET Identity table names and more. - // Add your customizations after calling base.OnModelCreating(builder); - } -} + // Register the entity sets needed by OpenIddict. + // Note: use the generic overload if you need + // to replace the default OpenIddict entities. + options.UseOpenIddict(); +}); ``` -> **Note:** if you change the default entity primary key (e.g. to `int` or `Guid` instead of `string`), make sure to use the `services.AddOpenIddict()`/`builder.UseOpenIddict()` overloads accepting a `TKey` generic argument: +> **Note:** if you change the default entity primary key (e.g. to `int` or `Guid` instead of `string`), make sure to use the `services.AddOpenIddict()` extension accepting a `TKey` generic argument and use the generic `options.UseOpenIddict()` overload: + ```csharp services.AddOpenIddict() .AddEntityFrameworkCoreStores() -``` -```csharp -protected override void OnModelCreating(ModelBuilder builder) { - // Register the entity sets needed by OpenIddict. - builder.UseOpenIddict(); +services.AddDbContext(options => { + // Configure the context to use Microsoft SQL Server. + options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]); - base.OnModelCreating(builder); - - // Customize the ASP.NET Identity model and override the defaults if needed. - // For example, you can rename the ASP.NET Identity table names and more. - // Add your customizations after calling base.OnModelCreating(builder); -} + options.UseOpenIddict(); +}); ``` - **Create your own authorization controller**: diff --git a/samples/Mvc.Server/Models/ApplicationDbContext.cs b/samples/Mvc.Server/Models/ApplicationDbContext.cs index 5de8fdcb..27cc8e52 100644 --- a/samples/Mvc.Server/Models/ApplicationDbContext.cs +++ b/samples/Mvc.Server/Models/ApplicationDbContext.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; namespace Mvc.Server.Models { public class ApplicationDbContext : IdentityDbContext { @@ -8,11 +7,6 @@ namespace Mvc.Server.Models { : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { - // Register the entity sets needed by OpenIddict. - // Note: use the generic overload if you need - // to replace the default OpenIddict entities. - builder.UseOpenIddict(); - base.OnModelCreating(builder); // Customize the ASP.NET Identity model and override the defaults if needed. diff --git a/samples/Mvc.Server/Startup.cs b/samples/Mvc.Server/Startup.cs index 87bee8f2..15ff9374 100644 --- a/samples/Mvc.Server/Startup.cs +++ b/samples/Mvc.Server/Startup.cs @@ -21,8 +21,15 @@ namespace Mvc.Server { services.AddMvc(); - services.AddDbContext(options => - options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"])); + services.AddDbContext(options => { + // Configure the context to use Microsoft SQL Server. + options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]); + + // Register the entity sets needed by OpenIddict. + // Note: use the generic overload if you need + // to replace the default OpenIddict entities. + options.UseOpenIddict(); + }); // Register the Identity services. services.AddIdentity()