在课堂上使用用户配置外键MVC 4创建新的配置文件 [英] Using UserProfile foreign key in class creates new profiles in MVC 4
问题描述
我试图让一个使用内置的MVC用户配置文件作为身份验证方法一个基本的网站。我使用MVC4.0和Entity Framework。
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.
作为一种尝试解决这个问题的一部分,我加入了游戏对象向用户简介的DbContext,这并没有在所有帮助,但是,我还是创建新用户每次插入一个新的游戏到数据库时。
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.
我的模型如下
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; }
}
我的新UsersContext是:
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!
推荐答案
更改用户配置
类是这样的:
[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 ...
这篇关于在课堂上使用用户配置外键MVC 4创建新的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!