1

I have trying to write this section in MVC 4 for a bit now and I just cant get the syntax right. I have a user class in my entities and every user has messages. On my messages the will be linked to the user via either sender or receiver. So this will have back and forth access methods. That way i can pull messages and either access through code first who the sender and receiver is, or access the the message they sent or received through the user.

I removed the modelBuilder info fromt he script and im back to fresh, If you need me to post any other info, plz let me know.

public class User
    {
            [Key]
            public Guid UserId { get; set; }

            public String Username { get; set; }
            public String Email { get; set; }
            public String Password { get; set; }
            public DateTime? BirthDate { get; set; }
            public String SecurityAnswer { get; set; }
            public String SecurityQuestion { get; set; }



            public ICollection<Message> PrivateMessages { get; set; }
            public ICollection<Message> SentMessages { get; set; }

    }



public class Message
    {
        [Key]
        public long Id { get; set; }

        public User Reciever { get; set; }
        public User Sender { get; set; }

        public string Title { get; set; }
        public string Message { get; set; }
        public DateTime PostDate { get; set; }
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
Random Kid
  • 25
  • 1
  • 7

1 Answers1

1

I think what you are looking for is the 'InverseProperty' data annotation. Check out this SO question: Navigation Property without Declaring Foreign Key

Community
  • 1
  • 1
DotNetWala
  • 6,574
  • 1
  • 18
  • 10
  • Thank you so much, I'm a little new to CodeFirst and this was a huge help. I was not aware of that attribute – Random Kid Mar 11 '13 at 13:46