Using UserProfile foreign key in class creates new profiles in MVC 4

情到浓时终转凉″ 提交于 2019-12-06 07:17:35

Change your UserProfile class like this:

[Table("UserProfile")]
public class UserProfile
{
    public UserProfile()
    {
        this.Games = new HashSet<Game>();            
    }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public int Balance { get; set; }

    public virtual ICollection<Game> Games { get; set; }
}

then insert your game like this:

Models.UsersContext uc = new UsersContext();

Game g = new Game();
g.UserId = WebSecurity.CurrentUserId;
g.Wager = int.Parse(BetQuantity);

uc.GamesList.Add(g);
uc.SaveChanges();

and note that you haven't declare any property for Wager in the Game model. I don't know what is it ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!