【问题标题】:EF Code-first MVC: Should I add fields to the default AspNetUsers table or create another 'UserInfo' table that relates to the AspNetUsers table?EF 代码优先 MVC:我应该将字段添加到默认 AspNetUsers 表还是创建另一个与 AspNetUsers 表相关的“UserInfo”表?
【发布时间】:2015-06-24 13:56:15
【问题描述】:

我正在使用代码优先实体框架制作一个简单的 MVC 应用程序。我正在尝试使用内置的用户注册/登录。它为我提供了默认的 AccountController 和 ASP User tables

我的其他实体是玩家、游戏和锦标赛:

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 提供these tables

每个登录的人都是玩家。我希望每个玩家都有一个密码和用户名。我应该删除我的Players 表并将Player 属性添加到提供的User 类和AspNetUsers 表吗?还是应该使用外键在Players 表和AspNetUsers 表之间建立关系?

这些选项中的一个或两个是否可能/更可取?

另外请注意,Players、Games 和 Tournaments 类中还有其他属性;我只是从基础开始。

非常感谢您抽出宝贵时间。如果我不清楚或者您是否需要任何其他信息,请告诉我。

编辑: 刚刚发现这个:http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

它显示了类似于我上面描述的两种方法。添加新的 UserInfo 表与向现有表中添加字段相比有什么优势?

【问题讨论】:

  • 如果Player类中的信息需要注册,那么它应该是User表的一部分

标签: c# asp.net-mvc entity-framework registration


【解决方案1】:

我会将此属性添加到播放器表中:

public virtual ApplicationUser User { get; set; }

其中 ApplicationUser 是 Visual Studio 开箱即用的表。 ApplicationUser 使用身份,因此当用户创建新玩家时,让 playerController 的 Create 操作将用户的用户 id 设置为播放器表中的玩家 id:

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

【讨论】:

    【解决方案2】:

    向 ApplicationUser 类添加属性。另外我建议你把它重命名为 Player,这样代码会更好地匹配域语言。

    【讨论】:

    • 非常感谢您与我们联系。我尝试将属性添加到 ApplicationUser FirstNameLastName 在迁移后没有出现在我的数据库中,并且在尝试使用名字和姓氏注册用户时得到500 Internal Server ErrorI made a post about it here。如果您对可能导致此问题的原因有任何想法,请告诉我。感谢您的宝贵时间
    • @ranquild 答案对 OP 完全没用。
    猜你喜欢
    • 1970-01-01
    • 2015-10-27
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2015-06-19
    • 2017-12-12
    • 1970-01-01
    相关资源
    最近更新 更多