question

yusuf2 avatar image
0 Votes"
yusuf2 asked AgaveJoe answered

What does this error message mean?

Hi

I'm working on this project and it's working.


But when I convert comments to code

 using Microsoft.AspNetCore.Mvc.RazorPages;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Logging;
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using WebApplication10.Data;
 using WebApplication10.Model;
    
 namespace WebApplication10.Pages
 {
     public class IndexModel : PageModel
     {
            
         //private readonly ILogger<IndexModel> _logger;
         //public IndexModel(ILogger<IndexModel> logger)
         //{
         //    _logger = logger;
         //}
    
         private readonly WebApplication10Context _db;
         public IndexModel(WebApplication10Context db)
         {
             _db = db;
         }
    
         public IEnumerable<Book> Books { get; set; }
         public async Task OnGet()
         {
             Books = await _db.Book.ToListAsync();
         }
     }
 }

I get this error.

InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'WebApplication10.Pages.IndexModel'. There should only be one applicable constructor.








dotnet-csharpdotnet-aspnet-core-razor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
1 Vote"
AgaveJoe answered

What does this error message mean?

Only one constructor executes. The dependency injection framework has no idea which constructor to execute. Add one constructor with both parameters you intend to inject. I think you'll be very interested in reading .NET 5 dependency injection fundamentals which covers standard patterns.


 public class IndexModel : PageModel
  {
      private readonly ILogger<IndexModel> _logger;
      private readonly WebApplication10Context _db;
         
      public IndexModel(WebApplication10Context db, ILogger<IndexModel> logger)
      {
          _db = db;
          _logger = logger;
      }
    
      public IEnumerable<Book> Books { get; set; }
      public async Task OnGet()
      {
          Books = await _db.Book.ToListAsync();
      }
  }



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much

0 Votes 0 ·