4

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

What I've got so far

I do currently have two models, with a one-to-many relationship, which work just fine:

_Events.cs (had to name it that way, because 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; }
}

The problem

Now, I would like to add another one-to-many relationship between Comment and the User from the Membership model, but can't seem to figure out how to do so.

The goal I would like to archieve is, that I can have a list of commments for each event and print out the user information for each comment. I tried several things, but could not get it to work yet. My last attempt looks like this:

public class Comment
{
    // snip - see above

    public virtual UserProfile User { get; set; }
}

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

Mansfield
  • 14,445
  • 18
  • 76
  • 112
sfey
  • 97
  • 2
  • 8
  • 1
    Wouldn't you also need to store the `UserId` in the comment if that's the case? Also, `_Event` isn't really a great class name I would consider changing this to something more specific. – James Aug 19 '13 at 12:18
  • SimpleMembership only binds to an existing model, not create a table for you. So, create your `UserProfile` as you normally would (with relationships) then just make sure it binds to the `Table` with `Id` & `UserName` columns. – Brad Christie Aug 19 '13 at 12:39
  • are you using default membership provider from Microsoft in your project? If yes, you need to implement membership provider for your project due to have relationships between models. Microsoft membership tables will store at separate place and you can't navigate between them. What is your purpose about using _EventId? – Amirhossein Mehrvarzi Aug 19 '13 at 12:42
  • @James I know, actually I'm using German names at the moment, _Event was only the translation I did before posting here. – sfey Aug 19 '13 at 12:55
  • @AmirHossein Mehrvarzi Yes, I'm using the default membership provider – sfey Aug 19 '13 at 12:56
  • @Brad Christie Would you mind posting a small example? As mentioned above I'm still a novice when it comes to ASP.NET. If I do understand you right, you mean I need to implement a own UserProfile class? – sfey Aug 19 '13 at 12:58
  • @sfey: Something like this: https://gist.github.com/bchristie/6268947 – Brad Christie Aug 19 '13 at 13:11

2 Answers2

2

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; }
    }
Xaxum
  • 3,545
  • 9
  • 46
  • 66
  • Thank you, this helped. Also thank you to @Brad Christie for a very smiliar solution. On a sidenote I had to add [InitializeSimpleMembership] to my controller, to get it all working. – sfey Aug 19 '13 at 15:10
1

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'.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
  • Thank you for this solution. I will have a closer look at this again for sure. For now the solution of @Xaxum did the trick. – sfey Aug 19 '13 at 15:12