简体   繁体   English

EF代码优先MVC:我应该将字段添加到默认的AspNetUsers表中还是创建另一个与AspNetUsers表相关的“ UserInfo”表?

[英]EF Code-first MVC: Should I add fields to the default AspNetUsers table or create another 'UserInfo' table that relates to the AspNetUsers table?

I am making a simple MVC application using code first entity framework. 我正在使用代码优先实体框架制作一个简单的MVC应用程序。 I'm trying to use the built in user registration/login. 我正在尝试使用内置的用户注册/登录。 It provides me the default AccountController and ASP User tables . 它为我提供了默认的AccountController和ASP User表

My other entities are Players, Games, and Tournaments: 我的其他实体是玩家,游戏和锦标赛:

public class Player
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Game> Games { get; set; }
    public List<Tournament> Tournaments { get; set; }
}

public class Game
{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<Player> Players { get; set; }
    public List<Tournament> Tournaments { get; set; } 
}

 public class Tournament
{
    public int Id { get; set; }
    public List<Player> Players { get; set; }
    public List<Game> Games { get; set; }

}

EF provides these tables . EF提供了这些表

Everyone who logs in is a player. 每个登录的人都是玩家。 I want each player to have a password and username. 我希望每个玩家都有一个密码和用户名。 Should I remove my Players table and add the Player properties to the provided User class and AspNetUsers table? 是否应该删除Players表并将Player属性添加到提供的User类和AspNetUsers表中? Or should I use a foreign-key to create a relationship between the Players table and the AspNetUsers table? 还是应该使用外键在Players表和AspNetUsers表之间创建关系?

Are either or both of those options possible/preferable? 这些选项中的一个或两个是否可能/更可取?

Also just a note, there will be additional properties in the Players, Games, and Tournaments classes; 另外,在Players,Games和Tournaments类中还将有其他属性。 I was just starting with the basics. 我只是从基础开始。

Thank you very much for your time. 非常感谢您的宝贵时间。 Let me know if I am being unclear or if you need any additional information. 如果我不清楚或需要任何其他信息,请告诉我。

EDIT: Just found this: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx 编辑:刚发现以下内容: http : //blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates。 aspx

It shows something like both methods I described above. 它显示了类似于我上面描述的两种方法。 What is the advantage of adding a new UserInfo table vs. adding fields to the existing table? 与在现有表中添加字段相比,添加新的UserInfo表有什么好处?

I would add this property to the player table: 我将此属性添加到播放器表:

public virtual ApplicationUser User { get; set; }

where ApplicationUser is the table that was given out of the box by Visual Studio. 其中ApplicationUser是Visual Studio开箱即用提供的表。 The ApplicationUser uses identity so when user creates a new player, have the playerController's Create action to set the user id of the user to the player id in the player table: ApplicationUser使用身份,因此当用户创建新的播放器时,请使用playerController的Create操作将用户的用户ID设置为播放器表中的播放器ID:

var userId = User.Identity.GetUserId();
Player.PlayerId = userId;

Add properties to ApplicationUser class. 将属性添加到ApplicationUser类。 Also I suggest you to rename it to Player, so code will better match domain language. 我也建议您将其重命名为Player,这样代码将更好地匹配域语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM