MVC Code First: One-to-Many Relationship Between Native Model and SimpleMembership User

I am currently working on an event calendar website, and I'm still a newbie when it comes to ASP.NET. I am using the MVC4 Framework, as well as EntityFramework (CodeFirst) and SimpleMembershipProvider, which comes with the MVC4 template. I also use Migrations - if it's from any interest.

What i still have

Currently, I have two one-to-many models that work very well:

_Events.cs (should have called it that way since the event is reserved)

public class _Event { public int _EventId { get; set; } public string Name { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public virtual List<Comment> comments { get; set; } } 

Comment.cs

 public class Comment { public int CommentId { get; set; } public string Text { get; set; } public int _EventId { get; set; } public virtual _Event _Event { get; set; } } 

Problem

Now I would like to add another relationship between the comment and the user from the Membership model, but I can’t figure out how to do this.

The goal I would like to archive is that I can have a list of suggestions for each event and print out user information for each comment. I tried a few things but couldn't get it to work. My last attempt looks like this:

 public class Comment { // snip - see above public virtual UserProfile User { get; set; } } 

I would like to thank you for any help in advance.

+4
source share
2 answers

Your comments should have UserId.

 public class Comment { // snip - see above public int UserId {get; set;} public virtual UserProfile User { get; set; } } 

You also need to establish a connection between your comment and your user, so there is something like this in your account controller.

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

In the MVC ASP.net project, if you want to use the default Microsoft membership provider, note , for which you need to implement a membership system, such as a role provider, to have relationships and navigation and more functionalists between your created models and the membership system . Microsoft stores user information in a separate place (for example, the database in the App_Data folder) in your project.

So, you need to save other models in the Microsoft storage location, work directly with Microsoft functions and set the connection string for this purpose OR introduce Microsoft membership to store user information in the appropriate database, for example, the NUGET package that implements codefirst membership provider in C # . You can install this package and learn how to write your own membership provider. More information will be found by searching for "user membership provider for MVC provider or aspnet service provider".

+1
source

Source: https://habr.com/ru/post/1497779/


All Articles