

Entity Framework Core: Why you should never name your DbContext 'IdentityDbConte...
source link: https://alexanderzeitler.com/articles/entity-framework-core-why-you-should-never-name-your-db-context-identitydbcontext/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Entity Framework Core: Why you should never name your DbContext 'IdentityDbContext'

This is more or less a follow up to the previous post.
As said in the post already, I was playing around with Startup.cs
style of application start up together with Entity Framework Core / ASP.NET Identity scaffolding.
So after I got Identity scaffolded, I re-added the Startup.cs
and wanted to move on with migrations.
My Program.cs
looked like this:
public class Program
{
public static void Main(
string[] args
)
{
CreateHostBuilder(args)
.Build()
.Run();
}
public static IHostBuilder CreateHostBuilder(
string[] args
) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
This was the Startup.cs
:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(
IHostEnvironment env
)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(
"appsettings.json",
optional: false,
reloadOnChange: true
)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(
IServiceCollection services
)
{
var connectionString = Configuration.GetConnectionString("IdentityDbContextConnection") ??
throw new InvalidOperationException(
"Connection string 'IdentityDbContextConnection' not found."
);
services.AddDbContext<IdentityDbContext>(options => options.UseNpgsql(connectionString));
services.AddDefaultIdentity<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<IdentityDbContext>();
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env
)
{
// Configure the HTTP request pipeline.
if (!env.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
}
);
}
}
The next step would be to create the first migration and seed the database:
dotnet ef migrations add CreateIdentitySchema
Straight forward, one might think... but no:
More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.
So, let's get a list of the contexts available using dotnet ef dbcontext list
:
dotnet ef dbcontext list
Build started...
Build succeeded.
Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
WebAppWithIdentity.Areas.Identity.Data.IdentityDbContext
Ok, let's try to use a full qualified name then:
dotnet ef database update --context WebAppWithIdentity.Areas.Identity.Data.IdentityDbContext
Build started... Build succeeded. Unable to create an object of type 'IdentityDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
That's not funny, tbh.
Well, let's go back to scaffolding and try this one (still with Startup.cs
in use):
dotnet aspnet-codegenerator identity -dc SecurityDbContext -u AppUser -f -gl -dbProvider sqlite
Result:
Building project ...
Finding the generator 'identity'...
Running the generator 'identity'...
RunTime 00:00:17.25
Umm... no let's list the contexts again:
dotnet ef dbcontext list
Build started...
Build succeeded.
WebAppWithIdentity.Areas.Identity.Data.SecurityDbContext
Well, that's interesting.
Now, let's try to run the migrations:
dotnet ef migrations add CreateIdentitySchema
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
And the final step: apply the changes to the actual database:
dotnet ef database update
Build started...
Build succeeded.
# shortened the output a bit here
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='20']
CREATE UNIQUE INDEX "UserNameIndex" ON "AspNetUsers" ("NormalizedUserName");
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='20']
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20230318144833_CreateIdentitySchema', '7.0.4');
Done.
Ok, lessons learned:
Never name your DbContent IdentityDbContext
Recommend
-
14
今天我们宣布EF Core 5.0发布第五个预览版。 1 先决条件 EF Core 5.0 的预览版要求 .NET Standard 2.1。这意味着: EF Core 5.0 在 .NET Core 3.1 上运行,不需要 .NET 5。根据 .NET 5 计划的改变...
-
11
How To Access SQL Generated By Entity Framework Core 3 – ChristianFindlay.comSkip to content Entity Framework Core (EF) converts exp...
-
25
Categories ProgrammingExecute Raw SQL Scripts in Entity Framework CoreMost of the time, Entity Framework Core will just do the ri...
-
9
March 23, 2021 How To Add A View To An Entity Framework Core DbContext ...
-
5
Implementing repository querying interface in EF Core DbContextMy last bold statement was that we don’t need custom unit of work and repository classes with Entity...
-
10
在 EF Core DbContext 使用 Dapper 並參與 Transaction-黑暗執行緒 寫 .NET 資料庫相關程式該用 EF/ORM 還是自己寫 SQL?就像手排車 vs 自排車,各有優劣及擅長的場合,亦各有支持者,我自己則是瀨尿牛丸派,單純 CRUD 用 EF (或自製 ORM) 享受強型別保...
-
8
將多個 EF Core DbContext 包成 Transaction-黑暗執行緒 之前介紹過將 EF Core DbContext 動作包入 Transaction,做法有兩種:呼叫 DbContext.Database.BeginTransactio...
-
3
EF Core DbContext 為什麼該註冊為 Scoped?-黑暗執行緒 我們都知道 ASP.NET Core 依賴注入(DI)容器註冊服務有三種生命週期選項:Singleton、Scoped、Transient,依先前
-
3
阅读须知:本文为入门介绍、指引文章,所示代码皆为最简易(或仅为实现功能)的演示示例版本,不一定切实符合个人(企业)实际开发需求。 一、DbContext生存期 DbContext 的生存期从创建实例时开始,并在释放实例时结束。 DbContex...
-
4
EF Core 多 DbContext 共用資料庫 EnsureCreated 失效問題-黑暗執行緒 讓 EF Core 依據 Entity 類別定義自動在資料庫新增資料表,主要有兩種做法:使用 Migrati...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK