✅ EF CORE, get request wont return related object

No description
42 Replies
restingphantom
restingphantom4mo 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
Pobiega4mo ago
can you show the code that makes the actual query? and is Portfolio here an owned entity, or a normal relational object?
restingphantom
restingphantom4mo 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
Pobiega4mo ago
ok, I see no include - this is probably just a missed include call
restingphantom
restingphantom4mo ago
i saw something on that on stack overflow
Pobiega
Pobiega4mo ago
try await _context.Ventures.Include(x => x.Portfolio).ToListAsync();
restingphantom
restingphantom4mo ago
i just didnt understand where to put it yes that works
Pobiega
Pobiega4mo ago
EF doesnt fetch relational objects by default, you need to explicitly state that you want them included
restingphantom
restingphantom4mo ago
would it also be possible to have some of the portfolio relations being returned like that
Pobiega
Pobiega4mo ago
yep! .ThenInclude(x => ...)
Angius
Angius4mo ago
But, ideally, you should .Select() instead
Pobiega
Pobiega4mo 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
restingphantom4mo ago
I know, but for my Proof of concept its fine for now
Pobiega
Pobiega4mo ago
sure thing. then .Include(...).ThenInclude(...) will work
Angius
Angius4mo 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
leowest4mo ago
also Guid? I dont think u want that on a pk
Pobiega
Pobiega4mo ago
why not? perfectly fine oh, nullable
leowest
leowest4mo ago
nullable Guid?
Pobiega
Pobiega4mo ago
😄 yeah remove the nullability
leowest
leowest4mo ago
:catgun:
Angius
Angius4mo ago
Sequential IDs gang represent
restingphantom
restingphantom4mo ago
Guid gets set in the db
Pobiega
Pobiega4mo ago
vogen ids all the way baby yeah but its nullable remove the nullability and you're fine
restingphantom
restingphantom4mo ago
yeah, otherwise it will set it to necisairy in the db and then I wont be able to post without ID
leowest
leowest4mo ago
yes and they are pk so they should never be null
Pobiega
Pobiega4mo ago
guid is a value type, it has a default non-null value (0000-000000...)
restingphantom
restingphantom4mo 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
Angius4mo ago
Tech2 💀 Not a list?
restingphantom
restingphantom4mo ago
that name isnt my choise
leowest
leowest4mo ago
specially with that many include
restingphantom
restingphantom4mo ago
😅
Angius
Angius4mo ago
Name is whatever, schema seems bad
restingphantom
restingphantom4mo ago
how do you mean?
Angius
Angius4mo ago
What if I want to list 67 skills?
restingphantom
restingphantom4mo ago
skills?
Angius
Angius4mo ago
Uh, techs
restingphantom
restingphantom4mo 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
Angius4mo ago
That makes more sense then Though the naming sucks ass
restingphantom
restingphantom4mo 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
Angius4mo ago
Yes A select
restingphantom
restingphantom4mo ago
aha let me try ill add that later Thnx everyone