Mawral
Mawral
CC#
Created by Mawral on 6/15/2024 in #help
Difficulty with Blazor Server chat app
Hi, I'm trying to build a straightforward group chat application with Entity Framework and Blazor Server, but I'm totally new to the latter and struggling to make it do what I want. I currently have the EF models set up, and a set of components including a 'ChatLog' for displaying messages, and a 'InputForm' for users to create new messages. The 'InputForm' can register a sent message and save it in the database, which will appear in the 'ChatLog' when the page is refreshed. But I'm clueless on how to make the 'ChatLog' update automatically with sent messages, and/or how I would use a SignalR Hub to make all other connected users load the new chat messages too. Could someone point me in the right direction?
5 replies
CC#
Created by Mawral on 3/1/2024 in #help
Identity/EF collection returns null
Hey, so I'm new to using Entity Framework. I tried to extend the Identity User model with a one-to-many relationship, like so:
//Model/AppUser.cs
public class AppUser : IdentityUser
{
public ICollection<Profile> Profiles { get; }
}

//Model/Profile.cs
public class Profile
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string AppUserId { get; set; }
public AppUser AppUser { get; set; }
}
//Model/AppUser.cs
public class AppUser : IdentityUser
{
public ICollection<Profile> Profiles { get; }
}

//Model/Profile.cs
public class Profile
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string AppUserId { get; set; }
public AppUser AppUser { get; set; }
}
From here, I want to make a razor page that shows a logged in user's 'Profiles' as a list, but going about it the most obvious way gives me a null value. Is there a step I'm missing for getting collection data, or some other error?
//some razor page's cshtml
public async Task<IActionResult> OnGetAsync()
{
StatusMessage = "";
var user = await _userManager.GetUserAsync(User);
if (user == null) {return NotFound($"User ID Not Found");}

Username = user.UserName; //works as expected
Profiles = user.Profiles; //null value
return Page();
}
//some razor page's cshtml
public async Task<IActionResult> OnGetAsync()
{
StatusMessage = "";
var user = await _userManager.GetUserAsync(User);
if (user == null) {return NotFound($"User ID Not Found");}

Username = user.UserName; //works as expected
Profiles = user.Profiles; //null value
return Page();
}
7 replies