C
Join ServerC#
help
❔ Entity Framework
PPOOPKAKA12/20/2022
im trying to make spotify like application but anytime i try to get the user who created the playlist it turns up null.
public List<PlaylistDTO> GetAllPlaylists()
{
using (var db = new SurroundDbContext())
{
return db.Playlists.Include(p => p.Creator).ToList();
}
}
public class PlaylistDTO
{
[Key]
public int Id { get; set; }
public string Name { get; set; } = null!;
public ICollection<SongDTO> Songs { get; set; } = null!;
[ForeignKey("UserId")]
public int CreatorId { get; set; }
public UserDTO Creator { get; set; } = null!;
}
public class UserDTO
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
public string? Username { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? VerPass;
public DateTime CreatedDate { get; set; }
public ICollection<PlaylistDTO> PlayLists { get; set; } = null!;
}
Pphaseshift12/20/2022
$code
MMODiX12/20/2022
To post C# code type the following:
```cs
// code here
```
Get an example by typing
If your code is too long, post it to: https://paste.mod.gg/
```cs
// code here
```
Get an example by typing
$codegif
in chatIf your code is too long, post it to: https://paste.mod.gg/
Pphaseshift12/20/2022
Please format code properly
PPOOPKAKA12/20/2022
how can i format it?
Pphaseshift12/20/2022
Read the message above
Pphaseshift12/20/2022
Include should work. Presume you don't have data in the db or something like that
PPOOPKAKA12/20/2022
{
"id": 1,
"name": "test123",
"songs": null,
"creator": {
"userID": 0,
"username": null,
"email": null,
"createdDate": "0001-01-01T00:00:00",
"playLists": null
}
},
{
"id": 10,
"name": "test123766",
"songs": null,
"creator": {
"userID": 0,
"username": null,
"email": null,
"createdDate": "0001-01-01T00:00:00",
"playLists": null
}
},
{
"id": 11,
"name": "test1qqq",
"songs": null,
"creator": {
"userID": 0,
"username": null,
"email": null,
"createdDate": "0001-01-01T00:00:00",
"playLists": null
}
}
PPOOPKAKA12/20/2022
i get this back
PPOOPKAKA12/20/2022
creator should be filled with a user
Pphaseshift12/20/2022
All the ids are 0, so looks like you didn't add the user data correctly
Pphaseshift12/20/2022
There's also no creatorId in your returned json ...
FFusedQyou12/20/2022
Why do you assign
null!
to everything?FFusedQyou12/20/2022
Anyway you probably forgot to specify how to map your creator?
FFusedQyou12/20/2022
That or the id is not saved at all. Did you check your database?
Pphaseshift12/20/2022
It's one way to avoid Compiler warnings
FFusedQyou12/20/2022
This is a null forgiving operator on null. How this is valid?
FFusedQyou12/20/2022
Is the whole point to throw an error when the value is not set?
Pphaseshift12/20/2022
The point is just to shut the compiler up. The value will be set by EF, so we don't care about it not being set in the constructor
PPOOPKAKA12/20/2022
exactly that
AAccord12/21/2022
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.