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

末鹿安然 提交于 2019-12-10 10:56:06

问题


I'm trying to make a basic website that uses the built in MVC user profile as the authentication method. I'm using MVC4.0 and Entity Framework.

I have a main data class that uses the UserProfile model as a foreign key. This class is a "game" class, for the purpose of relating a game to a certain user.

I've also added another member to the UserProfile class, as a necessity for this project.

Every time I try to add a new game, which has a user's profile in it as a foreign key, the server ends up making a new user profile altogether, even though I specifically made it so that it's the same user.

As a part of an attempt to fix this issue, I added the Game object to the user profile DbContext, this didn't help at all, however, and I still create new users every time I insert a new game to the database.

My models are as follow:

public class Game
{
    public int ID { get; set; }

    [ForeignKey("UserId")]
    public virtual UserProfile Profile { get; set; }

    public int UserId { get; set; }
}

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

My new UsersContext is:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Game> GamesList { get; set; }
}

My method for adding a new game is:

        Models.UsersContext uc = new UsersContext();
        UserProfile prof = uc.UserProfiles.Where(c=>c.UserName == HttpContext.User.Identity.Name).First();
        Game g = new Game();
        g.Profile = prof;
        g.Wager = int.Parse(BetQuantity);

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

I really have no idea what I'm doing wrong, any help would be enormously appreciated!


回答1:


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 ...



来源:https://stackoverflow.com/questions/14165581/using-userprofile-foreign-key-in-class-creates-new-profiles-in-mvc-4

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