✅ EF CORE, get request wont return related object

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