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

瘦欲@ 提交于 2019-12-03 12:57:33

问题


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. 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. Can anyone please be very kind to provide me a newbie guide to achieve this from a fresh made MVC5 Project. Thanks and sorry for the redundad question I've made.

PS. I can and I'm successful on creating everykind of relationshipt between two custom tables, but not between AspNetUsers and my Map table. Thanks very very much in advance.


回答1:


I have done exactly this on a number of projects

For instance i have a one to many relationship from ASPNetUsers to Notifications. So in my ApplicationUser class inside IdentityModels.cs i have

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

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

}

}




回答2:


http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html

this post contains your answer




回答3:


Here is a step by step solution with visuals (It uses inheritance):

https://practiceaspnet.wordpress.com/2015/04/03/adding-asp-net-identity-to-an-existing-entity-framework-asp-net-web-forms-application/



来源:https://stackoverflow.com/questions/29793926/one-to-many-relationship-between-aspnetusers-identity-and-a-custom-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!