简体   繁体   English

AspNetUsers(身份)与自定义表之间的一对多关系

[英]One to many relationship between AspNetUsers (Identity) and a custom table

I'm desperate trying to create an One to Many relationship between AspNetUsers table of Identity 2.0 and a custom table called Map (One user can have many maps, but a map can only have one user) I've tryied mostly every solutions available in this website and also lost many days trying other soulutions found in the web. 我迫切希望在Identity 2.0的AspNetUsers表和一个名为Map的自定义表之间创建一对多的关系(一个用户可以有很多地图,但一个地图只能有一个用户)我已经尝试了大多数可用的解决方案这个网站也失去了很多天尝试在网络上找到的其他soulutions。 I'm stuck. 我被卡住了。 Nothing seems to work for me. 似乎没有什么对我有用。 I'm new to MVC and EF so basically I think I need some sort of step-by-step guide. 我是MVC和EF的新手,所以基本上我认为我需要一些循序渐进的指南。 Can anyone please be very kind to provide me a newbie guide to achieve this from a fresh made MVC5 Project. 任何人都可以非常友好地为我提供一个新手指南,以实现这一点,从一个新的MVC5项目。 Thanks and sorry for the redundad question I've made. 谢谢,对不起我提出的冗余问题。

PS. PS。 I can and I'm successful on creating everykind of relationshipt between two custom tables, but not between AspNetUsers and my Map table. 我可以成功创建两个自定义表之间的关系,但不能在AspNetUsers和我的Map表之间创建。 Thanks very very much in advance. 非常感谢提前。

I have done exactly this on a number of projects 我在很多项目中做到了这一点

For instance i have a one to many relationship from ASPNetUsers to Notifications. 例如,我有一个从ASPNetUsers到Notifications的一对多关系。 So in my ApplicationUser class inside IdentityModels.cs i have 所以我在IdentityModels.cs里面的ApplicationUser类中有

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

My Notifications class has the reverse 我的通知类具有相反的功能

public virtual ApplicationUser ApplicationUser { get; set; }

By default EF will then create a cascade delete from Notification to AspNetUsers which i dont want - so i also have this in my Context class 默认情况下,EF将创建从Notification到AspNetUsers的级联删除,我不想要 - 所以我也在我的Context类中有这个

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

Just remember the definition for AspNetUSers is extended in the ApplicationUser class inside IdentityModels.cs that is generated for you by visual studios scaffolding. 请记住,AspNetUSers的定义是在Visual Studio脚手架为您生成的IdentityModels.cs中的ApplicationUser类中进行了扩展。 Then treat it as any other class/table in your app 然后将其视为应用中的任何其他类/表

UPDATE - here are examples of full models 更新 - 以下是完整模型的示例

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; }

}

} }

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

相关问题 自定义 Model 与 AspNetUsers 之间的一对多关系 - One to Many relationship between Custom Model and AspNetUsers AspNetUsers(Identity)与自定义数据库中的其他表之间存在多对多的关系 - Many to many relationship between AspNetUsers (Identity) and other tables in a custom database 与ASP.NET身份表和自定义表有多对多的关系 - Many to many relationship with ASP.NET Identity Table and Custom Table 如何在Aspnetusers表和自定义表之间建立关系? - How do I make a relationship between my Aspnetusers table and a custom table? AspNetUsers的ID作为单独表中的外键,一对一的关系 - AspNetUsers' ID as Foreign key in separate table, one-to-one relationship .NET Identity 将用户 ID 从 aspnetusers 添加到没有控制器的自定义表 - .NET Identity Add userid from aspnetusers to custom table without controller 创建与AspNetUsers到其他表的关系 - Create Relationship to AspNetUsers to other table 多对多关系中的自定义关系表 - Custom relationship table in many to many relationship EF代码首先与ASP.NET身份表建立多对多关系 - EF Code First Many-to-Many Relationship with ASP.NET Identity Table at one Side 如何使用它们之间的链接表建立多对一关系? - How to make a Many-to-One relationship with a link table in between?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM