C
C#2y ago
arch_il

❔ A problem with database in asp dotnet

I have class Market that has Owner as one object. With migration everything worked just fine. In database Market table has value OwnerId and writing to that table from program works just fine, but when i try to get those values it shows me null in place of Owner. What do I do?
27 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
arch_il
arch_ilOP2y ago
this is Market
public class Market
{
[Key]
public int Id { get; set; }
public List<Product> Products { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public decimal NetWorth { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public string Location { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public MarketCompany OwnerCompany { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public Owner Owner { get; set; }
}
public class Market
{
[Key]
public int Id { get; set; }
public List<Product> Products { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public decimal NetWorth { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public string Location { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public MarketCompany OwnerCompany { get; set; }
[Required(ErrorMessage = "{0} is required!")]
public Owner Owner { get; set; }
}
this is where I get them from database
[HttpGet("[action]/{id}")]
public async Task<ActionResult<Market>> GetById(int id)
{
Market? market = await db.Market.FirstOrDefaultAsync(x => x.Id == id);

if (market == null)
return NotFound();

return new ObjectResult(market);
}
[HttpGet("[action]/{id}")]
public async Task<ActionResult<Market>> GetById(int id)
{
Market? market = await db.Market.FirstOrDefaultAsync(x => x.Id == id);

if (market == null)
return NotFound();

return new ObjectResult(market);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
arch_il
arch_ilOP2y ago
you mean using? I have all the Entities namespace.
using MarketNetwork.API.Entities;
using MarketNetwork.API.Entities;
I have this line on top.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
db.Market.Include(x => x.Owner).FirstOrDefaultAsync(x => x.Id == id); Or use lazy loaded relations
arch_il
arch_ilOP2y ago
thx a lot it worked yeah did that. thanks to you too will it still work when i have 1 to many relation using List?
FusedQyou
FusedQyou2y ago
Yes It just includes whatever relation you have Usually your List would be an ICollection in the model definition
arch_il
arch_ilOP2y ago
thanks a lot
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
Lol The dude's gonna read your link, don't worry. No problem with giving the actual reason No idea what's so wrong with that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
I would agree if it's a big problem. This is just a small mistake he made A bit shitty to provide all the documentation when it's such a small thing
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
So the next time you see something you don't agree with, you explain why you don't agree with it instead of responding with "don't do this". I simply gave an alternative without providing context Lazy loading, as the link explains, can indeed make or break your application But considering he would have to look how to do it, I can imagine there being a mention of that wherever he looks for it
Angius
Angius2y ago
So... 1. Don't ever use lazy-loading. It'll query your database each time for each related property. And why query the database 273 times if you can query it once? 2. Don't use .Include() if you're fetching data for display only. Use .Select() and select the data into a separate DTO/ViewModel class. That way, you only get the data you need
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
You're kind of required to use Include. I would advice on not using it altogether, and getting related data seperately. This is more scalable instead of getting everything and everything related at once, since you probably wont use half of it in most endpoints Automapper has a ProjectTo<> extension that is pretty useful and saves the hassle of creating the Select statements yourself No, you need to call Include, and then Select
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
Automapper can't even load related data. I believe it's a bug
Angius
Angius2y ago
No you don't There's zero need for includes with a select
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
FusedQyou
FusedQyou2y ago
Guess it makes sense Don't think I ever read that it was possible
Angius
Angius2y ago
var p = await _ctx.People
.Where(p => p.Id == id)
.Select(p => new PersonDto {
Id = p.Id,
Name = p.Name,
Blogposts = p.Blogposts
.Select(b => new BlogpostDto {
Title = b.Title,
Slug = b.Slug
}),
Links = p.Links
})
.FirstOrDefaultAsync();
var p = await _ctx.People
.Where(p => p.Id == id)
.Select(p => new PersonDto {
Id = p.Id,
Name = p.Name,
Blogposts = p.Blogposts
.Select(b => new BlogpostDto {
Title = b.Title,
Slug = b.Slug
}),
Links = p.Links
})
.FirstOrDefaultAsync();
This works perfectly fine For a Person like
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Blogpost> Blogposts { get; set; }
public List<Link> Links { get; set; }
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Blogpost> Blogposts { get; set; }
public List<Link> Links { get; set; }
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2y ago
Fetches all the data you need, and only the data you need, and does it in a single query Lazy loading and .Include() eat your heart out
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server