将asp.net mvc标识与其他自定义表集成以构建一个edmx图

时间:2014-12-15 16:37:11

标签: c# asp.net asp.net-mvc

我想问一个关于asp.net身份和集成自定义表的问题。我理解使用asp.net身份系统进行用户登录和注册以及与用户成员身份和帐户管理问题相关的其他操作更容易,更快捷。例如,如果我想建立一个博客,并且因为众所周知,在博客中用户必须注册,并且用户与许多帖子和许多评论相关联。使用默认的身份系统,我不知道如何将数据库中的其他表包含到必须处理成员资格的现有表中,这样我就可以有一个edmx图来映射身份表和其他表之间的所有关系。博客。  为了让它更清晰,我开始使用我自己的会员系统的自定义实现,到目前为止我一直很好,但我注意到我没有使用asp.net的默认身份系统而错过了很多。假设我有以下表格

dbo.Categories (a post belong to a category and a category contains many post

dbo.Posts

dbo.Replies

dbo.UserProfile 在我提到userId的情况下,AuthorId是相同的,并且仅引用Users表中的用户的id(如果在asp.net身份中有类似的东西)。我

我怎么能有这样的东西,而不是我自己的dbo.Userprofile,我想要asp.net身份,然后从中生成一个模型。我希望我的问题有道理。

2 个答案:

答案 0 :(得分:0)

您必须在此方案中使用的中心类是IdentityUser。基于此,您可以创建用户类,使用特定属性扩展IdentityUser类,这些属性不属于基类(例如Gender)。它可能看起来像这样:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,   ApplicationUserRole, ApplicationUserClaim>
{
  public string Gender { get; set; }

  public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
  {
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    return userIdentity;
  }
}

因为您使用整数作为键的类型,您必须进行一些自定义。 James在this thread中提供的答案向您展示了如何做到这一点。最后要做的是将现有表链接到新用户表。这可以通过简单地设置外键来完成。

<强>更新

当然,你应该遵循克里斯提到的提示。错过了提及。

答案 1 :(得分:0)

现在和停止使用EDMX 一样好。它在EF7中被弃用并完全消失。身份使用Code First,因此您希望包含在同一上下文中的任何模型也应该是Code First。有关即将发生的更改以及您应该执行的操作的详细信息,请参阅the announcement on the ADO.NET blog