MVC Code First: One-to-many relationship between own model and SimpleMembership user

主宰稳场 提交于 2019-12-05 14:16:49

You need to have UserId in your Comments as so.

public class Comment
{
    // snip - see above

    public int UserId {get; set;}
    public virtual UserProfile User { get; set; }
}

You will also need to set the relationship between your Comment and your User so in your account controller have something like this.

public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

In an ASP.net MVC project if you want to use default Microsoft Membership Provider, note that you need to implement membership system like role provider to have relationships and navigation and more functionalists between your created models and membership system. Microsoft Stores User related info at the separate place (like databases in App_Data folder) in your project.

So you need to store other models to Microsoft storage place, work with Microsoft functions directly and set connection string for this purpose OR implement Microsoft Membership to store User Info at relevant database like this NUGET Package that implements codefirst membership provider in C#. You can install this package and learn to write your own membership provider. More helps will be found by searching 'custom membership provider for MVC or aspnet membership provider'.

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