✅ EF CORE, get request wont return related object

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