1 Replies - 1902 Views - Last Post: 12 December 2012 - 06:31 PM Rate Topic: -----

#1 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Entity framework - reference same entity twice

Posted 10 December 2012 - 06:44 AM

Hi All,

sorry if the title is a bit unclear/ambiguous but I am unsure of how so get the following code bound via Fluent API (if it's even required)

public class ChatUser 
{
    [Key]
    public int ChatUserId { get; set; }
    public string Name { get; set; }

    public bool IsOnline { get; set; }
    // other properties

    // navigation properties
    public ICollection<ChatMessage> Messages { get; set; }
}

public class ChatMessage
{
    [Key]
    public int ChatMessageId { get; set; }
    public string Message { get; set; }

    public int UserFromId { get; set; }
    public int UserToId { get; set; }

    public DateTime DateSent { get; set; }

    // navigation properties
    public ChatUser UserFrom { get; set; }
    public ChatUser UserTo { get; set; }
}



The idea is that I need to be able to access a list of "Messages" from a user and when I've got a Message object, I need to be able to access both the UserFrom and UserTo so I can get the properties such as Name but the problem I am having is to get the two "ChatUser" objects to be bound to the UserFromId and UserToId - they both come up as null.

If anyone could point me in the correct direction/any links that I can learn from that would be appreciated :)

PS if there is proper terminology for what I am trying to achieve could someone let me know - I was unsure of what to Google to solve my issue!

Is This A Good Question/Topic? 0
  • +

Replies To: Entity framework - reference same entity twice

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: Entity framework - reference same entity twice

Posted 12 December 2012 - 06:31 PM

I would just use a simple List(Of ChatMessage) then you can assess the list. You should not mix the classes. Isolate the user and that class has a List(Of ChatMessage) in the ChatMessage class you have could use an Enum or boolean to set the ChatMessage as to if it is incoming or outgoing. You then can LINQ the incoming or outgoing messages.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1