@@ -26,28 +26,7 @@ You can install the following packages through your IDE or go to the nuget link
To use EFCore, you need a DbContext to access everything in your database. The DbContext will look like this. Here is an example entity to show you how you can add more entities yourself later on.
```cs
// ApplicationDbContext.cs
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<UserEntity> Users { get; set; } = null!;
> To learn more about creating the EFCore model, visit the following [link](https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli#create-the-model)
@@ -56,17 +35,7 @@ public class UserEntity
To add your newly created DbContext to your Dependency Injection container, simply use the extension method provided by EFCore to add the context to your container. It should look something like this
> You can find out how to get your connection string [here](https://www.connectionstrings.com/npgsql/standard/)
@@ -80,30 +49,10 @@ To learn more about migrations, visit the official Microsoft documentation [here
You can now use the DbContext wherever you can inject it. Here's an example on injecting it into an interaction command module.
```cs
using Discord;
public class SampleModule : InteractionModuleBase<SocketInteractionContext>
{
private readonly ApplicationDbContext _db;
public SampleModule(ApplicationDbContext db)
{
_db = db;
}
[SlashCommand("sample", "sample")]
public async Task Sample()
{
// Do stuff with your injected DbContext
var user = _db.Users.FirstOrDefault(x => x.Id == Context.User.Id);
...
}
}
```
[!code-csharp[DBContext injected into interaction module](samples/InteractionModuleDISample.cs)]
## Using a different database provider
Here's a couple of popular database providers for EFCore and links to tutorials on how to set them up. The only thing that usually changes is the provider inside of your `DbContextOptions`
For serilog to log discord events correctly, we have to map the discord `LogSeverity` to the serilog `LogEventLevel`. You can modify your log method to look like this.
For serilog to log Discord events correctly, we have to map the discord `LogSeverity` to the serilog `LogEventLevel`. You can modify your log method to look like this.
[!code-csharp[Modifying your log method](samples/ModifyLogMethod.cs)]
## Testing
@@ -92,9 +39,7 @@ If you run your application now, you should see something similar to this
Now that you have set up Serilog, you can use it everywhere in your application by simply calling
```cs
Log.Debug("Your log message, with {Variables}!", 10) // This will output "[21:51:00 DBG] Your log message, with 10!"
> Depending on your configured log level, the log messages may or may not show up in your console. Refer to [Serilog's github page](https://github.com/serilog/serilog/wiki/Configuration-Basics#minimum-level) for more information about log levels.
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.