Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Entity framework - reference same entity twice

$
0
0
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!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>