null
null
CC#
Created by null on 11/21/2023 in #help
[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:
public class Post
{
public int Id { get; set; }
public List<Tag> Tags { get; } = new();
public List<PostTag> PostTags { get; } = new();
}

public class Tag
{
public int Id { get; set; }
public List<Post> Posts { get; } = new();
public List<PostTag> PostTags { get; } = new();
}

public class PostTag
{
public int PostId { get; set; }
public int TagId { get; set; }
public string CustomPayload { get; set; }
}
public class Post
{
public int Id { get; set; }
public List<Tag> Tags { get; } = new();
public List<PostTag> PostTags { get; } = new();
}

public class Tag
{
public int Id { get; set; }
public List<Post> Posts { get; } = new();
public List<PostTag> PostTags { get; } = new();
}

public class PostTag
{
public int PostId { get; set; }
public int TagId { get; set; }
public string CustomPayload { get; set; }
}
MyDbContext:
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public virtual DbSet<Post> Posts { get; set; }
public virtual DbSet<Tag> Tags { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public virtual DbSet<Post> Posts { get; set; }
public virtual DbSet<Tag> Tags { get; set; }
}
when I try to create the initial migration I get the following error:
Unable to create a 'DbContext' of type ''. The exception 'The entity type 'PostTag' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'
Unable to create a 'DbContext' of type ''. The exception 'The entity type 'PostTag' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'
When adding a primary key on the PostTag model:
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'PostTag' for entity type 'PostTag' since it is being used for entity type 'PostTag (Dictionary<string, object>)' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'PostTag' on the primary key properties and pointing to the primary key on another entity type mapped to 'PostTag'.
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'PostTag' for entity type 'PostTag' since it is being used for entity type 'PostTag (Dictionary<string, object>)' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'PostTag' on the primary key properties and pointing to the primary key on another entity type mapped to 'PostTag'.
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
CC#
Created by null on 11/30/2022 in #help
❔ [.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
CC#
Created by null on 10/5/2022 in #help
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
CC#
Created by null on 9/12/2022 in #help
[.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