C
C#•2mo ago
Neeyo

Access database field in one to many relationship in asp net core

In my example I have Polls and PollOptions tables, I can access PollOptions.Poll table but not the other way around
No description
No description
5 Replies
FusedQyou
FusedQyou•2mo ago
Please share the relevant classes, otherwise there's not much to say apart from "Make sure the Poll class has a visible PollOptions collection"
Neeyo
Neeyo•2mo ago
C#
using System.ComponentModel.DataAnnotations.Schema;

namespace API.Entities;

[Table("PollOptions")]
public class PollOption
{
public int Id { get; set; }
public required string Option { get; set; }
public int VotesCount { get; set; }
public int PollId { get; set; }
public Poll Poll { get; set; } = null!;
public ICollection<UserPollOption> UserPollOption { get; set; } = [];
}

using System.ComponentModel.DataAnnotations.Schema;

namespace API.Entities;

[Table("Polls")]
public class Poll
{
public int Id { get; set; }
public required string Title { get; set; }
public required string Content { get; set; }
public ICollection<PollOption> PollOptions { get; set; } = [];
public int GroupId { get; set; }
public Group Group { get; set; } = null!;
}
C#
using System.ComponentModel.DataAnnotations.Schema;

namespace API.Entities;

[Table("PollOptions")]
public class PollOption
{
public int Id { get; set; }
public required string Option { get; set; }
public int VotesCount { get; set; }
public int PollId { get; set; }
public Poll Poll { get; set; } = null!;
public ICollection<UserPollOption> UserPollOption { get; set; } = [];
}

using System.ComponentModel.DataAnnotations.Schema;

namespace API.Entities;

[Table("Polls")]
public class Poll
{
public int Id { get; set; }
public required string Title { get; set; }
public required string Content { get; set; }
public ICollection<PollOption> PollOptions { get; set; } = [];
public int GroupId { get; set; }
public Group Group { get; set; } = null!;
}
C#
//Poll - PollOption
builder.Entity<PollOption>()
.HasOne(x => x.Poll)
.WithMany(x => x.PollOptions)
.HasForeignKey(x => x.PollId)
.IsRequired();
C#
//Poll - PollOption
builder.Entity<PollOption>()
.HasOne(x => x.Poll)
.WithMany(x => x.PollOptions)
.HasForeignKey(x => x.PollId)
.IsRequired();
Angius
Angius•2mo ago
Well, you can access the properties that you have Poll.PollOptions is a collection It has the properties of a collection
FusedQyou
FusedQyou•2mo ago
You can't access PollOptions.Poll because your collection has many polls So if you want to access it, you have to ask yourself in which way For example, if you want to return all the polls that exist in the poll options, you can use LINQ. LINQ has handy shorthands that make it easy to access collections amongst other things In this case you use Select to select an inner property/field/method in a collection You already used LINQ's Where method btw, so it's similar x => x.PollOptions.Select(y => y.Poll)
Neeyo
Neeyo•2mo ago
Yeah, I tried doing something really stupid there, thanks 😄
Want results from more Discord servers?
Add your server