【问题标题】:One to many relationship between AspNetUsers (Identity) and a custom tableAspNetUsers (Identity) 和自定义表之间的一对多关系
【发布时间】:2015-06-29 21:30:30
【问题描述】:

我非常想在 Identity 2.0 的 AspNetUsers 表和一个名为 Map 的自定义表之间创建一对多关系(一个用户可以有多个地图,但一个地图只能有一个用户)我已经尝试过几乎每个该网站上提供的解决方案,也浪费了很多天尝试在网络上找到的其他解决方案。我被困住了。似乎没有什么对我有用。我是 MVC 和 EF 的新手,所以基本上我认为我需要一些分步指南。任何人都可以非常友好地为我提供一个新手指南,以从一个新制作的 MVC5 项目中实现这一目标。感谢并为我提出的多余问题感到抱歉。

附言。我可以并且我成功地在两个自定义表之间创建了各种关系,但在 AspNetUsers 和我的 Map 表之间却没有。提前非常感谢。

【问题讨论】:

标签: c# mysql entity-framework asp.net-mvc-5 asp.net-identity-2


【解决方案1】:

我在很多项目中都做到了这一点

例如,我有一个从 ASPNetUsers 到 Notifications 的一对多关系。因此,在 IdentityModels.cs 中的 ApplicationUser 类中,我有

public virtual ICollection<Notification> Notifications { get; set; }

我的通知类有相反的

public virtual ApplicationUser ApplicationUser { get; set; }

默认情况下,EF 将创建一个从通知到我不想要的 AspNetUsers 的级联删除 - 所以我的 Context 类中也有这个

modelBuilder.Entity<Notification>()
    .HasRequired(n => n.ApplicationUser)
    .WithMany(a => a.Notifications)
    .HasForeignKey(n => n.ApplicationUserId)
    .WillCascadeOnDelete(false);

请记住 AspNetUSers 的定义是在 IdentityModels.cs 内的 ApplicationUser 类中扩展的,该类是由 Visual Studio 脚手架为您生成的。然后将其视为您应用中的任何其他类/表

更新 - 以下是完整模型的示例

public class ApplicationUser : IdentityUser
{

    [StringLength(250, ErrorMessage = "About is limited to 250 characters in length.")]
    public string About { get; set; }

    [StringLength(250, ErrorMessage = "Name is limited to 250 characters in length.", MinimumLength=3)]
    public string Name { get; set; }

    public DateTime DateRegistered { get; set; }
    public string ImageUrl { get; set; }

    public virtual ICollection<Notification> Notifications { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
    }
}


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

    public int? CommentId { get; set; }

    public string ApplicationUserId { get; set; }

    public DateTime DateTime { get; set; }

    public bool Viewed { get; set; }

    public virtual ApplicationUser ApplicationUser { get; set; }

    public virtual Comment Comment { get; set; }

}

}

【讨论】:

  • 谢谢.. 我想这就是我需要的,你能给我提供你的通知模型的完整示例和你的模型类 ApplicationUser 的相关代码:IdentityUser,我正在尝试实现相同的目标,但我不能..非常感谢!
  • 非常感谢!这个周末我会试试的!我是否还必须启用迁移和更新某些内容?
  • 是的 - 所有这些都假设您首先使用代码进行迁移,否则对模型的更改将不会反映在数据库中。 HTH
  • 我希望我不会打扰你,但我很新……在你的具体情况下,我必须更新哪些字段?
  • 这对你有用吗?如果是这样,您为什么不接受它作为答案?我现在和你的情况一样。而且您应该始终牢记,该站点也是未来来者的资源,您不应该自私。如果对您有帮助,请将其标记为答案..
【解决方案2】:

【讨论】:

  • 非常感谢您的帮助,我很感激,不幸的是我仍然无法实现我想要的。基本上我需要创建一个新的 Db 表(地图),它具有简单的一对多关系AspNetUser 之间(一个 AspNetUser 可以有多个 Maps)。如果你有时间可以给我一些代码吗?非常感谢
【解决方案3】:

【讨论】:

  • 非常感谢您的帮助,我很感激,不幸的是我仍然无法实现我想要的。基本上我需要创建一个新的 Db 表(地图),它具有简单的一对多关系AspNetUser 之间(一个 AspNetUser 可以有多个 Maps)。如果你有时间可以给我一些代码吗?非常感谢。
猜你喜欢
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
相关资源
最近更新 更多