✅ EF CORE, get request wont return related object

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