Skip to content

Commit 6c87e27

Browse files
committed
Add a note about ASP.NET Core Identity migrations
1 parent 7da78f8 commit 6c87e27

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,51 @@ There are several common situations when this exception can be thrown:
6666
- **Mitigation**: Add a new migration, examine its contents to locate the cause, and replace the dynamic data with a static, hardcoded value in the model. The migration should be recreated after the model is fixed. If dynamic data has to be used for seeding consider using [the new seeding pattern](/ef/core/what-is-new/ef-core-9.0/whatsnew#improved-data-seeding) instead of `HasData()`.
6767
- The last migration was created for a different provider than the one used to apply the migrations.
6868
- **Mitigation**: This is an unsupported scenario. The warning can be suppressed using the code snippet below, but this scenario will likely stop working in a future EF Core release. The recommended solution is [to generate a separate set of migrations for each provider](xref:core/managing-schemas/migrations/providers).
69-
- The migrations are generated or chosen dynamically by replacing some of the EF services.
70-
- **Mitigation**: The warning is a false positive in this case and should be suppressed:
71-
69+
- The migrations are generated, modified or chosen dynamically by replacing some of the EF services.
70+
- **Mitigation**: The warning is a false positive in this case and should be suppressed:
7271
`options.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning))`
72+
- You are using ASP.NET Core Identity and change options that affect the model, such as:
73+
74+
```csharp
75+
.AddDefaultIdentity<ApplicationUser>(options =>
76+
{
77+
options.Stores.SchemaVersion = IdentitySchemaVersions.Version2;
78+
options.Stores.MaxLengthForKeys = 256;
79+
options.SignIn.RequireConfirmedAccount = false;
80+
})
81+
```
82+
83+
- **Mitigation**: To make sure that the options are applied consistently the app needs to be specified as the startup project when running the EF tools or, alternatively, `IDesignTimeDbContextFactory` needs to be implemented in the project containing the `DbContext`:
84+
85+
```C#
86+
public class DatabaseContextDesignTimeFactory : IDesignTimeDbContextFactory<DatabaseContext>
87+
{
88+
public DatabaseContext CreateDbContext(string[] args)
89+
{
90+
var services = new ServiceCollection();
91+
AddIdentity(services);
92+
var serviceProvider = services.BuildServiceProvider();
93+
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
94+
optionsBuilder.UseApplicationServiceProvider(serviceProvider);
95+
optionsBuilder.UseSqlServer();
96+
return new DatabaseContext(optionsBuilder.Options);
97+
}
98+
99+
public static IServiceCollection AddIdentity(IServiceCollection services)
100+
{
101+
services.AddDefaultIdentity<ApplicationUser>(options =>
102+
{
103+
options.Stores.SchemaVersion = IdentitySchemaVersions.Version2;
104+
options.Stores.MaxLengthForKeys = 256;
105+
options.SignIn.RequireConfirmedAccount = false;
106+
})
107+
.AddRoles<IdentityRole>()
108+
.AddEntityFrameworkStores<DatabaseContext>();
109+
110+
return services;
111+
}
112+
}
113+
```
73114

74115
If your scenario doesn't fall under any of the above cases and adding a new migration creates the same migration each time or an empty migration and the exception is still thrown then create a small repro project and [share it with the EF team in a new issue](https://github.com/dotnet/efcore/issues/new/choose).
75116

0 commit comments

Comments
 (0)