populus
populus
CC#
Created by populus on 2/12/2023 in #help
✅ ForeignKey Entity Framework and Identity
Thanks ZZ
24 replies
CC#
Created by populus on 2/12/2023 in #help
✅ ForeignKey Entity Framework and Identity
This utilizes "Fluent API"? Searching in NuGet and wondering whether it's "Henk Mollema" or "Jeremy Skinner" who made it?
24 replies
CC#
Created by populus on 2/12/2023 in #help
✅ ForeignKey Entity Framework and Identity
Maybe I can create a "Participator" class and a "Attending" class. Both with "List<Account>" fields.
24 replies
CC#
Created by populus on 2/12/2023 in #help
✅ ForeignKey Entity Framework and Identity
I was hoping I could declare a List<Guid> and have the DB populate it with every associated AccountId one-by-one as they become relevant. "it is the POST that needs a FK..." - My attempt at this was to create these List<Guid>. What is your recommended course of action?
24 replies
CC#
Created by populus on 2/12/2023 in #help
✅ ForeignKey Entity Framework and Identity
Here's the latest error message among many.
System.InvalidOperationException: 'There are multiple navigations in entity type 'Post' which are pointing to same set of properties 'Account' using a [ForeignKey] attribute: 'PostParticipantsIds', 'UsersAttendingIds'.'
System.InvalidOperationException: 'There are multiple navigations in entity type 'Post' which are pointing to same set of properties 'Account' using a [ForeignKey] attribute: 'PostParticipantsIds', 'UsersAttendingIds'.'
24 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
Oh really, I must have navigated around that somehow. I installed it through the NuGet package manager and no code was provided by default.
23 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
I am on the final stretches of my C# programming course and I guess MVC was what we learned. I don't know how I got Razor pages in the mix, but I think MVC is the way to go for me. But let's suppose I did want to do the Razor Pages code way, isn't it essentially just moving the code in there? I am using Aspnet.core.Identity and entityframeworkcore for Auth and DB respectively.
23 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
I did not realize these were different frameworks. But it seems like using controllers and .cshtml files with the "@page" directive works? Because that's how I got it to work partially during my work on this. You would recommend me to troubleshoot this on google by using "MVC" as part of the keyword(s) searched?
23 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
For example, there is a "/Pages/Shared/" which was searched, and even though I tried putting the "Login.cshtml" and "Register.cshtml" there it did not find them.
23 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
So /Pages/ is not a synonym for /Views/ then? Because that's what I got when creating the project ("scaffolding"(?)).
23 replies
CC#
Created by populus on 2/4/2023 in #help
❔ View cannot be found.
Hey, so /Pages/ was the default folder added by the scaffolding. I tried creating a /Views/ folder and moving the relevant files there but I ran into some 405 issues so I assumed /Pages/ was the correct way. I can't quite puzzle it together.
23 replies
CC#
Created by populus on 2/3/2023 in #help
❔ Unsupported Media Type - Razor page
Here's the "Account"-model:
using Microsoft.AspNetCore.Identity;

namespace CommunityWebsite_Lexicon_Project.Models
{
public class Account : IdentityUser
{
public int Id { get; set; }
public string? UserName { get; set; }
public string? Email { get; set; }
private string Password { get; set; }
public async Task SetPassword(string password)
{
Password = new PasswordHasher<Account>().HashPassword(this, password);
}

public async Task<bool> VerifyPassword(string password)
{
var result = new PasswordHasher<Account>().VerifyHashedPassword(this, Password, password);
return result == PasswordVerificationResult.Success;
}
}
}
using Microsoft.AspNetCore.Identity;

namespace CommunityWebsite_Lexicon_Project.Models
{
public class Account : IdentityUser
{
public int Id { get; set; }
public string? UserName { get; set; }
public string? Email { get; set; }
private string Password { get; set; }
public async Task SetPassword(string password)
{
Password = new PasswordHasher<Account>().HashPassword(this, password);
}

public async Task<bool> VerifyPassword(string password)
{
var result = new PasswordHasher<Account>().VerifyHashedPassword(this, Password, password);
return result == PasswordVerificationResult.Success;
}
}
}
6 replies
CC#
Created by populus on 2/3/2023 in #help
❔ Unsupported Media Type - Razor page
Here's the "LoginModel":
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace CommunityWebsite_Lexicon_Project.Models
{
public class LoginModel
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? PasswordConfirm { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace CommunityWebsite_Lexicon_Project.Models
{
public class LoginModel
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? PasswordConfirm { get; set; }
}
}
6 replies
CC#
Created by populus on 2/3/2023 in #help
❔ Unsupported Media Type - Razor page
Here's the "AccountController":
public async Task<IActionResult> Register(LoginModel submittedAccountModel)
{
if (ModelState.IsValid)
{
Account createdUser = new Account() // Create account object.
{
UserName = submittedAccountModel.Username,
Email = submittedAccountModel.Email,
};

Regex regex = new Regex(@"^[a-zA-Z0-9]+$"); // Only letters and digits allowed.
if (!regex.IsMatch(createdUser.UserName))
{
ModelState.AddModelError("NickName", "Only letters and digits are allowed in the NickName field.");
}

try // Setting the password.
{
await createdUser.SetPassword(submittedAccountModel.Password);
} catch (Exception)
{
throw new Exception("Error.");
} finally // Verify the password.
{
await createdUser.VerifyPassword(submittedAccountModel.PasswordConfirm);
}

IdentityResult result = await _userManager.CreateAsync( // Create the Account-Identity object.
createdUser, createdUser.PasswordHash
);

if (result.Succeeded)
{
await _accountRepository.AddAsync(createdUser); // Save the Account-Identity object.
return RedirectToAction("Login");
//return Ok();
} else
{
return View(submittedAccountModel);
}
} else
{
return BadRequest();
}
}
public async Task<IActionResult> Register(LoginModel submittedAccountModel)
{
if (ModelState.IsValid)
{
Account createdUser = new Account() // Create account object.
{
UserName = submittedAccountModel.Username,
Email = submittedAccountModel.Email,
};

Regex regex = new Regex(@"^[a-zA-Z0-9]+$"); // Only letters and digits allowed.
if (!regex.IsMatch(createdUser.UserName))
{
ModelState.AddModelError("NickName", "Only letters and digits are allowed in the NickName field.");
}

try // Setting the password.
{
await createdUser.SetPassword(submittedAccountModel.Password);
} catch (Exception)
{
throw new Exception("Error.");
} finally // Verify the password.
{
await createdUser.VerifyPassword(submittedAccountModel.PasswordConfirm);
}

IdentityResult result = await _userManager.CreateAsync( // Create the Account-Identity object.
createdUser, createdUser.PasswordHash
);

if (result.Succeeded)
{
await _accountRepository.AddAsync(createdUser); // Save the Account-Identity object.
return RedirectToAction("Login");
//return Ok();
} else
{
return View(submittedAccountModel);
}
} else
{
return BadRequest();
}
}
6 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
So what you're saying is that 'MSSQLLocalDB' is not a built-in variable but a customizable one to what I set it? The thing is though that I am seeing the databases from my other project(s) in the Server Object Explorer. I don't know any other lines or places where I have written 'MSSQLLocalDB' so that's why I assumed it was like saying "Use this protocol". After deleting the 'Migrations' folder and running ' dotnet ef migrations add InitialCreate ' followed up with 'dotnet ef database update' and re-building the project I notice that I don't get the configuration log in the console, suggesting that the database update isn't happening at even that stage. Even though I get a new 'Migrations' folder.
15 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
Thanks, I was pretty sure I remembered everything needed to get going with this database but there is just something minor I am overlooking here somewhere. The page you linked seems to be talking about a relevant but at the same time irrelevant branch of what I'm doing. I say that because I haven't read about LocalDB specifically before and I have succeeded in creating a database with Entity Framework Core and Microsoft SQL DB previously. I did start this VS project as a console app and I am wondering whether or not that means some pre-requisites aren't configured, for example 'builder.services' which I haven't fully grasped the functionality of yet. If that is the case please let me know. Am working on reading through old lecture PDF's but for now this thread is still in need of input.
15 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
You would have to clarify for me what a database instance is. The line you're referring to is copied from a functional database setup on the same PC and environment. I think it means Microsoft SQL Local DB and should correspond to the packages included with the NuGet install of Entity Framework Core and the related packages.
15 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
Yes, same error but a little different. I am using Entity Framework core 7.0.1. In SQL Server Object Explorer the database does not show up so it's not getting created in the first place. But to my understanding it is supposed to get created automatically by the code I have provided, no? This is the first time I'm attempting to create a EF database aside from the time(s) we did it in school.
15 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
or whatever the issue is, can't seem to figure it out
15 replies
CC#
Created by populus on 1/3/2023 in #help
❔ Entity Framework - SqlException
Shouldn't this database get created automatically?
15 replies