✅ EF CORE, get request wont return related object

No description
42 Replies
restingphantom
restingphantomOP8mo ago
Venture
c#
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

[Table("Venture")]
public class Venture
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

public string Name { get; set; }

public Portfolio? Portfolio { get; set; }
}
c#
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

[Table("Venture")]
public class Venture
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

public string Name { get; set; }

public Portfolio? Portfolio { get; set; }
}
Pobiega
Pobiega8mo ago
can you show the code that makes the actual query? and is Portfolio here an owned entity, or a normal relational object?
restingphantom
restingphantomOP8mo ago
c#

// GET: api/Venture
[HttpGet]
public async Task<ActionResult<IEnumerable<Venture>>> GetVentures()
{
if (_context.Ventures == null)
{
return NotFound();
}

return await _context.Ventures.ToListAsync();
}
c#

// GET: api/Venture
[HttpGet]
public async Task<ActionResult<IEnumerable<Venture>>> GetVentures()
{
if (_context.Ventures == null)
{
return NotFound();
}

return await _context.Ventures.ToListAsync();
}
venture has one or zero portfolios
Pobiega
Pobiega8mo ago
ok, I see no include - this is probably just a missed include call
restingphantom
restingphantomOP8mo ago
i saw something on that on stack overflow
Pobiega
Pobiega8mo ago
try await _context.Ventures.Include(x => x.Portfolio).ToListAsync();
restingphantom
restingphantomOP8mo ago
i just didnt understand where to put it yes that works
Pobiega
Pobiega8mo ago
EF doesnt fetch relational objects by default, you need to explicitly state that you want them included
restingphantom
restingphantomOP8mo ago
would it also be possible to have some of the portfolio relations being returned like that
Pobiega
Pobiega8mo ago
yep! .ThenInclude(x => ...)
Angius
Angius8mo ago
But, ideally, you should .Select() instead
Pobiega
Pobiega8mo ago
^ the ideal way to do this, especially in a web api, is to .Select into a DTO you dont want to return your actual entity models directly to the user
restingphantom
restingphantomOP8mo ago
I know, but for my Proof of concept its fine for now
Pobiega
Pobiega8mo ago
sure thing. then .Include(...).ThenInclude(...) will work
Angius
Angius8mo ago
For a proof of concept I'd be selecting into anonymous objects tbh Defining them later Rather than including all over the place
leowest
leowest8mo ago
also Guid? I dont think u want that on a pk
Pobiega
Pobiega8mo ago
why not? perfectly fine oh, nullable
leowest
leowest8mo ago
nullable Guid?
Pobiega
Pobiega8mo ago
😄 yeah remove the nullability
leowest
leowest8mo ago
:catgun:
Angius
Angius8mo ago
Sequential IDs gang represent
restingphantom
restingphantomOP8mo ago
Guid gets set in the db
Pobiega
Pobiega8mo ago
vogen ids all the way baby yeah but its nullable remove the nullability and you're fine
restingphantom
restingphantomOP8mo ago
yeah, otherwise it will set it to necisairy in the db and then I wont be able to post without ID
leowest
leowest8mo ago
yes and they are pk so they should never be null
Pobiega
Pobiega8mo ago
guid is a value type, it has a default non-null value (0000-000000...)
restingphantom
restingphantomOP8mo ago
c#
return await _context.Ventures
.Include(x => x.Portfolio)
.ThenInclude(x => x.Tech1)
.Include(x => x.Portfolio)
.ThenInclude( x => x.Tech2)
.ToListAsync();
c#
return await _context.Ventures
.Include(x => x.Portfolio)
.ThenInclude(x => x.Tech1)
.Include(x => x.Portfolio)
.ThenInclude( x => x.Tech2)
.ToListAsync();
btw like this?
Angius
Angius8mo ago
Tech2 💀 Not a list?
restingphantom
restingphantomOP8mo ago
that name isnt my choise
leowest
leowest8mo ago
specially with that many include
restingphantom
restingphantomOP8mo ago
😅
Angius
Angius8mo ago
Name is whatever, schema seems bad
restingphantom
restingphantomOP8mo ago
how do you mean?
Angius
Angius8mo ago
What if I want to list 67 skills?
restingphantom
restingphantomOP8mo ago
skills?
Angius
Angius8mo ago
Uh, techs
restingphantom
restingphantomOP8mo ago
ow no thats also what I thought when I saw that tech one is technology one and has sub technologies called tech2 so you choose category tech1 sub category tech2
Angius
Angius8mo ago
That makes more sense then Though the naming sucks ass
restingphantom
restingphantomOP8mo ago
yup, I know but thats how my internship did it
c#
public async Task<ActionResult<IEnumerable<Venture>>> GetVentures()
{
if (_context.Ventures == null) return NotFound();

return await _context.Ventures
.Include(x => x.Portfolio)
.Include(x => x.Portfolio).ThenInclude(x => x.Tech1)
.Include(x => x.Portfolio).ThenInclude(x => x.Tech2)
.Include(x => x.Portfolio).ThenInclude(x => x.Sdgs)
.Include(x => x.Portfolio).ThenInclude(x => x.Industry)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments).ThenInclude(x => x.InevestmentType)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments).ThenInclude(x => x.Series)
.ToListAsync();
}
c#
public async Task<ActionResult<IEnumerable<Venture>>> GetVentures()
{
if (_context.Ventures == null) return NotFound();

return await _context.Ventures
.Include(x => x.Portfolio)
.Include(x => x.Portfolio).ThenInclude(x => x.Tech1)
.Include(x => x.Portfolio).ThenInclude(x => x.Tech2)
.Include(x => x.Portfolio).ThenInclude(x => x.Sdgs)
.Include(x => x.Portfolio).ThenInclude(x => x.Industry)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments).ThenInclude(x => x.InevestmentType)
.Include(x => x.Portfolio).ThenInclude(x => x.Investments).ThenInclude(x => x.Series)
.ToListAsync();
}
I feel like there is a better way of doing this
Angius
Angius8mo ago
Yes A select
restingphantom
restingphantomOP8mo ago
aha let me try ill add that later Thnx everyone
Want results from more Discord servers?
Add your server