Trying to scaffold a controller for ASP .NET api using MongoDB but get this message

Anyone have any idea what causes this?
No description
2 Replies
Honza K.
Honza K.10mo ago
This shit gets broken quite often actually... I never managed to fix it, I always created a new empty project and did the scaffold action there and just copied the stuff out to my actual project 😄
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Thanks, I'll try this! I managed to scaffold the controller by bypassing the mongoDB registration to the apps builderServices using temporary InMemory db. However I am now faced with another issue. Since the mongo database won't register to the app, the endpoints do not show up in the Swagger UI. What are my options from here? This is what the context looks like.
internal class PostContext : DbContext
{
public DbSet<PostModel> Posts { get; init; }

private const string connectionString = "mongodb://localhost:27017";

public PostContext(DbContextOptions<PostContext> options) : base(options)
{
}

public static PostContext Create(IMongoDatabase database) =>
new(new DbContextOptionsBuilder<PostContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
.Options);


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var client = new MongoClient(connectionString);

optionsBuilder.UseMongoDB(client, "WebCollection"); // This row does not seem to work
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PostModel>().ToCollection("posts");
}

}
internal class PostContext : DbContext
{
public DbSet<PostModel> Posts { get; init; }

private const string connectionString = "mongodb://localhost:27017";

public PostContext(DbContextOptions<PostContext> options) : base(options)
{
}

public static PostContext Create(IMongoDatabase database) =>
new(new DbContextOptionsBuilder<PostContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
.Options);


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var client = new MongoClient(connectionString);

optionsBuilder.UseMongoDB(client, "WebCollection"); // This row does not seem to work
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PostModel>().ToCollection("posts");
}

}
The problem was solved by switching from EFCore to a standalone IMongoDB. EFCore does not seem to have stable support for MongoDB at this time.
Want results from more Discord servers?
Add your server