I am currently using Databases for an A-Level Computing project. As the question says, I am currently having an Error when I try and save data to my database (UserGameKey). The Database has two columns; GameKey, which is the Unique Key, and the UserGameKeyID, which is a foreign key equal to a user's ID from the Identity Database.
This issue is one that has been encountered by several people already - however, the answers given almost always suggest the issue arises because the Foreign Key doesn't yet exist within the initial Database from which it is derived. In my case, that's not true - at least it doesn't appear to be - and as such every time I try and save a new Record to UserGameKey, the error;
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UserGameKey_Project_With_IdentityUser_UserGameKeyID"
occurs.
Here is my /play.cshtml, where the data is attempting to be saved from;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Razor;
using Project_With_Identity.Hubs;
using Microsoft.AspNetCore.Identity;
using Project_With_Identity.Areas.Identity.Data;
using Project_With_Identity.Models;
namespace Project_With_Identity.Pages
{
public class PlayModel : PageModel
{
private readonly UserManager<Project_With_IdentityUser> _userManager;
private readonly SignInManager<Project_With_IdentityUser> _signInManager;
private readonly UserGameKeyContext _gamekeycontext;
public PlayModel(
UserManager<Project_With_IdentityUser> userManager,
SignInManager<Project_With_IdentityUser> signInManager,
UserGameKeyContext gameKeyContext)
{
_userManager = userManager;
_signInManager = signInManager;
_gamekeycontext = gameKeyContext;
}
[BindProperty]
public string gameKey { get; set; }
public async Task<IActionResult> OnPostCreateGame()
{
await createGame();
UserGameKey NewUserGameKey = new UserGameKey { };
NewUserGameKey.UserGameKeyID = _userManager.GetUserId(User);
NewUserGameKey.GameKey = gameKey;
if(_userManager.FindByIdAsync(NewUserGameKey.UserGameKeyID) != null)
{
try
{
_gamekeycontext.UserGameKeys.Add(NewUserGameKey);
_gamekeycontext.SaveChanges();
}
catch (Exception e)
{
Console.Write(e.Message);
return Redirect("./Error");
}
}
var URL = "/Game/" + gameKey + ".cshtml";
return Redirect(URL);
}
//edited for brevity.
}
}
And here is my UserGameKey Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Project_With_Identity.Areas.Identity.Data;
namespace Project_With_Identity.Models
{
public class UserGameKey
{
[Key]
public string GameKey { get; set; }
[ForeignKey("Project_With_IdentityNavi")]
public string UserGameKeyID { get; set; }
public Project_With_IdentityUser Project_With_IdentityNavi { get; set; }
}
}
My main focus is getting /play.cshtml to save the new Gamekey and the currently logged in User's ID to the UserGameKey Database, so that the User can use the same Gamekey to access the same game more than once.
Any advice would be appreciated, thanks!