āœ… Unable to create a 'DbContext' of type 'ApplicationDbContext'.

hi šŸ‘‹ i'm trying to structure my project with DDD model but getting this error: Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The entity type 'IdentityUserLogin<string>' requires a primary key to be defined. (this used to work before restructuring) i'm adding db context in Project.Web/Program.cs:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("ApplicationDbContext") ??
throw new InvalidOperationException(
"Connection string 'BlogContext' not found."
)
)
);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("ApplicationDbContext") ??
throw new InvalidOperationException(
"Connection string 'BlogContext' not found."
)
)
);
db context in Project.Infrastructure.Data:
namespace Project.Infrastructure.Data;

public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: IdentityDbContext<ApplicationUser>(options) {
public DbSet<Post> Post { get; set; } = default!;
public DbSet<TagPost> TagPost { get; set; } = default!;
public DbSet<Comment> Comment { get; set; } = default!;

public DbSet<Product> Product { get; set; } = default!;
public DbSet<Category> Category { get; set; } = default!;
public DbSet<CartItem> CartItem { get; set; }
namespace Project.Infrastructure.Data;

public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: IdentityDbContext<ApplicationUser>(options) {
public DbSet<Post> Post { get; set; } = default!;
public DbSet<TagPost> TagPost { get; set; } = default!;
public DbSet<Comment> Comment { get; set; } = default!;

public DbSet<Product> Product { get; set; } = default!;
public DbSet<Category> Category { get; set; } = default!;
public DbSet<CartItem> CartItem { get; set; }
and ApplicationUser in Project.Domain.Entities.Identity
public class ApplicationUser : IdentityUser {
public ICollection<Post> Posts { get; } = [];
public ICollection<Comment> Comment { get; } = [];
}
public class ApplicationUser : IdentityUser {
public ICollection<Post> Posts { get; } = [];
public ICollection<Comment> Comment { get; } = [];
}
i've moved migration files to Project.Infrastructure/Data/Migrations but now i can't do databtase update or make new migrations, or run the app
8 Replies
amirreza šŸ‡®šŸ‡·šŸ‡µšŸ‡ø
ā”œā”€ā”€ Project.Domain
ā”œā”€ā”€ Project.Infrastructure
ā”œā”€ā”€ Project.Web
ā”œā”€ā”€ Project.sln
└── Project.sln.DotSettings.user
ā”œā”€ā”€ Project.Domain
ā”œā”€ā”€ Project.Infrastructure
ā”œā”€ā”€ Project.Web
ā”œā”€ā”€ Project.sln
└── Project.sln.DotSettings.user
Fayoka
Fayoka•6d ago
I am not sure honestly as I am not that experienced but don't you just have to add the attribute [Key] to the model IdentityUserLogin
amirreza šŸ‡®šŸ‡·šŸ‡µšŸ‡ø
IdentityUserLogin is made by asp, not me and it has a primary key already
Fayoka
Fayoka•6d ago
i asked gippity, maybe the issiue is that you have to update the location of the migrations in the DI:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("ApplicationDbContext")
?? throw new InvalidOperationException("Connection string 'ApplicationDbContext' not found."),
b => b.MigrationsAssembly("Project.Infrastructure") // šŸ‘ˆ IMPORTANT
)
);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("ApplicationDbContext")
?? throw new InvalidOperationException("Connection string 'ApplicationDbContext' not found."),
b => b.MigrationsAssembly("Project.Infrastructure") // šŸ‘ˆ IMPORTANT
)
);
Otherwise I got no clue either and I am sorry if you already did that to lol šŸ™‚
Unknown User
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
amirreza šŸ‡®šŸ‡·šŸ‡µšŸ‡ø
🫠 🫠 yup
Fayoka
Fayoka•6d ago
god damnit haha, the hallucinations as well when i tried to explain to gippity what the correct solution was as well xd
amirreza šŸ‡®šŸ‡·šŸ‡µšŸ‡ø
thank you šŸ™Œ

Did you find this page helpful?