null
[EF Core 8] Many-to-many relationship with payload
Hi all,
I am trying to create a many-to-many relationship as described in https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-and-join-table-with-payload
My models are the following:
MyDbContext:
when I try to create the initial migration I get the following error:
When adding a primary key on the
PostTag
model:
I have a few questions:
1) What am I missing and cannot make this relationship function?
2) Even if somehow the tables are produced from the migration, will the PostTag
table be able to return the CustomPayload
and how?
3) Is this supposed to be the correct way to supply the join table with a user defined payload
?7 replies
❔ [.NET 6][Odata 8] IQueryable Async
Hello,
I am currently trying to implement an OData Web API.
OData works incredibly well with IQueryable as it applies the ODataQueryOptions directly to the database call.
I am trying to figure out, if it is possible to make the controller action async.
What I already have implemented:
[EnableQuery]
[HttpGet("Users")]
public IActionResult GetUsers([FromServices] UsersDbContext usersContext)
{
return new OkObjectResult(usersContext.Users);
}
What I am trying to achieve:
[EnableQuery]
[HttpGet("Users")]
public async Task<IActionResult> GetUsers([FromServices] UsersDbContext usersContext)
{
return new OkObjectResult(await usersContext.Users); <--- something async
}
3 replies
Refactor constructor to be used with DI [Answered]
Hello,
Is it possible to refactor the following constructor in order to be available for DI without the need to create a new instance?
DatabaseCaller.cs
namespace WebApplication1.Database
{
public class DatabaseCaller : IDatabaseCaller
{
private readonly MyDbContext myDbContext;
private readonly int someValueFromConfig;
public DatabaseCaller(MyDbContext myDbContext,
int someValueFromConfig)
{
this.myDbContext = myDbContext;
this.someValueFromConfig = someValueFromConfig;
}
public async Task GetValueFromDb()
{
//query the database, code omitted
await Task.CompletedTask;
}
}
public interface IDatabaseCaller
{
Task GetValueFromDb();
}
}
Program.cs//...
builder.Services.AddScoped<IDatabaseCaller, DatabaseCaller>();
WeatherForecastController.cs
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly AppSettings appSettings;
private readonly DatabaseCaller databaseCaller;
private readonly MyDbContext myDbContext;
public WeatherForecastController(
IOptions<AppSettings> appSettings,
MyDbContext myDbContext)
{
this.myDbContext = myDbContext;
this.appSettings = appSettings.Value;
this.databaseCaller = new DatabaseCaller(myDbContext, this.appSettings.MyValue);
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task GetAsync()
{
await databaseCaller.GetValueFromDb();
await Task.CompletedTask;
}
}
36 replies
[.NET Core 6][HttpClient] Storing JWT in cache
Hello,
I am trying to figure out a way to store a JWT for a service to service network communication.
Currently, I have a background service (IBackgroundService) that requests and stores the JWT in the server's cache.
When the JWT expires, the background service requests a new token.
The JWT is given to the associated HttpClient, that requires it for communicating with the other service, by constructor injection.
Is this considered a good practice?
Are there any available information on the topic?
2 replies