Hi MIPAKTEH_1,
Please be sure you have Installed the
Microsoft.EntityFrameworkCore.Design
,Microsoft.EntityFrameworkCore.SqlServer
, Microsoft.EntityFrameworkCore
package.
And the registration of the DbContext should be placed in the following location in Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped<ICommentService, CommentService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// 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.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
Besides, I suggest you move the model design and interface to the separate file, cause the name of the CommentService
class will conflict with the variable name you set here @inject ICommentService CommentService
. If you do not want to move them, just change the variable name like below:
@inject ICommentService _commentService
@code{
private void SubmitComment(MouseEventArgs e, int buttonNumber)
{
var comment = new ImageComment { Title = images[buttonNumber].Title, Comment = images[buttonNumber].Comment };
_commentService.AddCommentAsync(comment); //change here...
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena