Create a CRUD
with a Api
, and everything works fine, I can successfully manipulate the data from my controller
, create a class
call LogsE
, to be able to save the exceptions there but when saving them, it throws me the following error.
System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.'
I don't know why when I use that class
my DB provider doesn't recognize me. but in the controller it works perfectly.
Annex CodeController
public class LibraryController : ControllerBase
{
private readonly ApiDbContext context;
public LibraryController(ApiDbContext context) => this.context = context;
[HttpPut("{id}")]
public IActionResult BookShefLinePut(BookShefLine bookShefLine, int id)
{
var MSJ = string.Empty;
try
{
BookShefLine bksl = context.bookshelfline.FirstOrDefault(b => b.id == id);
if (bksl == null)
{
MSJ = "ID DOESN'T EXIST";
return new JsonResult(MSJ);
}
bksl.shelfid = bookShefLine.shelfid;
context.SaveChanges();
MSJ = "ROWS AFFECTED";
}
catch (Exception ex)
{
MSJ = ex.Message + " --- " + ex.InnerException + " --- " + ex.StackTrace;
LogsE error = new LogsE(); //AQUI HAGO MI INTANCIA PARA MI CLASE
error.SaveError(MSJ); // Y GUARDAR AL MOMENTO DE UNA EXCEPCION
MSJ = "Ocurrio un Error Interno";
}
return new JsonResult(MSJ);
}
}
Annex Code of my DBContext
public class ApiDbContext : DbContext
{
public ApiDbContext()
{
}
public ApiDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<BookShefLine> bookshelfline { get; set; }
public DbSet<LogsError> logserror { get; set; }
}
Annex Code of my class to save the exceptions
public class LogsE : DbContext
{
private readonly ApiDbContext context;
public LogsE(ApiDbContext context)
{
this.context = context;
}
public LogsE()
{
context = new ApiDbContext();
}
public void SaveError(string MSJ)
{
var ErrorL = new LogsError();
ErrorL.messag = MSJ;
ErrorL.datetime = DateTime.Now;
context.logserror.Add(ErrorL); //EXPLOTA EN ESTA LINEA
context.SaveChanges();
}
}
I attach my LogsE model
public class LogsError
{
public int id { get; set; }
public string messag { get; set; }
public DateTime datetime { get; set; }
}
I don't know what I'm doing wrong or what I might be missing, I appreciate your suggestions to solve my problem
EDIT
Adding the following code in my ApiDbContext I manage to solve but it is not a good idea to leave my connection data to my DB in that class, that is what the appsetting.config file is for
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Server=127.0.0.1; Port=5455; Database=TEST; UserId=USER; Password=12345; Timeout=15; ");
base.OnConfiguring(optionsBuilder);
}
I recommend the following, make your own DbContext in this case LoggerDbContext which inherits from DbContext, you use the Constructor that receives the DbContextOptions as a parameter, to obtain the connection string from the appsettings or from some external configuration file, doing that you have to configure this new DbContext (LoggerDbContext) in your Startup, by having this, you create an interface, this case IMyLogger, with the SaveError() method, you implement this interface as you see fit. Now here is the importance of using dependency injection and not doing news(). In the constructor of the class that implements IMyLogger, you inject the LoggerDbContext, and now all you have to do is configure this new service (IMyLogger) in your startup, this way where you need to use your logger, you just inject IMyLogger and that's it, the code would look something like this:
Remember to add your services to the startup:
You can use it in your controllers like this:
My final recommendation is that you use a library to log your errors either in your database or in a file, for example you can use Serilog.