C
C#2y ago
poopkaka

❔ Entity Framework

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!;
}
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!;
}
13 Replies
phaseshift
phaseshift2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
phaseshift
phaseshift2y ago
Please format code properly
poopkaka
poopkaka2y ago
how can i format it?
phaseshift
phaseshift2y ago
Read the message above Include should work. Presume you don't have data in the db or something like that
poopkaka
poopkaka2y ago
{
"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
}
}
{
"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
}
}
i get this back creator should be filled with a user
phaseshift
phaseshift2y ago
All the ids are 0, so looks like you didn't add the user data correctly There's also no creatorId in your returned json ...
FusedQyou
FusedQyou2y ago
Why do you assign null! to everything? Anyway you probably forgot to specify how to map your creator? That or the id is not saved at all. Did you check your database?
phaseshift
phaseshift2y ago
It's one way to avoid Compiler warnings
FusedQyou
FusedQyou2y ago
This is a null forgiving operator on null. How this is valid? Is the whole point to throw an error when the value is not set?
phaseshift
phaseshift2y ago
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
poopkaka
poopkaka2y ago
exactly that
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