C
C#3y ago
Stay

EF Core not fetching sub-entities by Id [Answered]

I'm kinda new to EF Core so maybe the terminology is not right. I have the following models: Player:
public sealed class Player
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id { get; set; } = null!;
public string Username { get; set; } = null!;
public Group Group { get; set; } = null!;
}
public sealed class Player
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id { get; set; } = null!;
public string Username { get; set; } = null!;
public Group Group { get; set; } = null!;
}
Group:
public sealed class Group
{
public string Id { get; set; } = null!;
public string? ParentId { get; set; }
public string Name { get; set; } = null!;
public string[] Permissions { get; set; } = Array.Empty<string>();
}
public sealed class Group
{
public string Id { get; set; } = null!;
public string? ParentId { get; set; }
public string Name { get; set; } = null!;
public string[] Permissions { get; set; } = Array.Empty<string>();
}
And tables: Players:
CREATE TABLE IF NOT EXISTS public.players
(
"Id" character varying(36) COLLATE pg_catalog."default" NOT NULL,
"Username" character varying(64) COLLATE pg_catalog."default",
"GroupId" character varying(36) COLLATE pg_catalog."default",
CONSTRAINT players_pkey PRIMARY KEY ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.players
OWNER to postgres;
CREATE TABLE IF NOT EXISTS public.players
(
"Id" character varying(36) COLLATE pg_catalog."default" NOT NULL,
"Username" character varying(64) COLLATE pg_catalog."default",
"GroupId" character varying(36) COLLATE pg_catalog."default",
CONSTRAINT players_pkey PRIMARY KEY ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.players
OWNER to postgres;
Groups:
CREATE TABLE IF NOT EXISTS public.groups
(
"Id" character varying(36) COLLATE pg_catalog."default" NOT NULL,
"ParentId" character varying(36) COLLATE pg_catalog."default",
"Name" character varying(64) COLLATE pg_catalog."default",
"Permissions" character varying(64)[] COLLATE pg_catalog."default",
CONSTRAINT groups_pkey PRIMARY KEY ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.groups
OWNER to postgres;
CREATE TABLE IF NOT EXISTS public.groups
(
"Id" character varying(36) COLLATE pg_catalog."default" NOT NULL,
"ParentId" character varying(36) COLLATE pg_catalog."default",
"Name" character varying(64) COLLATE pg_catalog."default",
"Permissions" character varying(64)[] COLLATE pg_catalog."default",
CONSTRAINT groups_pkey PRIMARY KEY ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.groups
OWNER to postgres;
It works fine when adding a player, it inserts the GroupId as i would expect it to, but when fetching
var player = await _context.Players.FirstOrDefaultAsync(x => x.Id == request.Id);
var player = await _context.Players.FirstOrDefaultAsync(x => x.Id == request.Id);
Group is null Why is this happening?
2 Replies
Stay
StayOP3y ago
You are absolutely right. My bad
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server