Ahmed Mujtaba
Ahmed Mujtaba

Reputation: 2248

association between entities - EntityFramework

I'm a beginner with EF and although I have managed to use it successfully before, I can't understand a simple problem I'm facing right now.

I have the following tables in my project.

MessageUser Table

        public MessageUser()
    {
        this.Conversation = new HashSet<Conversation>();
    }
    public int MessageUserID { get; set; }
    public string ConnectionID { get; set; }
    public string UserName { get; set; }
    public ICollection<Conversation> Conversation { get; set; }

ChatMessage Table

    public class ChatMessage
{
    public int ChatMessageID { get; set; }

    public string Username { get; set; }

    public string Message { get; set; }

    public bool DeliveryStatus { get; set; }

    public DateTime CreatedAt { get; set; }

    public int ConversationID { get; set; }

    [ForeignKey("ConversationID")]
    public Conversation Conversation { get; set; }
}

Conversation Table

    public class Conversation
{
    public Conversation()
    {
        ChatMessages = new List<ChatMessage>();
        MessageUsers = new List<MessageUser>();
    }
    public int ConversationID { get; set; }
    public DateTime CreatedAt { get; set; }
    public ICollection<MessageUser> MessageUsers { get; set; }
    public ICollection<ChatMessage> ChatMessages { get; set; }
}

The 'ChatMessage' table and 'Conversation' table have a one-many relationship whereas 'Conversation' and 'MessageUser' have many-many relationship. I'm trying to save the data as follows:

        DataModel db = new DataModel();
        List<MessageUser> ConnectedUsers = db.MessageUsers.ToList();
        ChatMessage message = new ChatMessage { CreatedAt = DateTime.Now, DeliveryStatus = true, Message = "Hello My name is Mujtaba", Username = "Mujtaba" };
        Conversation conversation = new Conversation();
        conversation.ChatMessages.Add(message);
        conversation.CreatedAt = DateTime.Now;
        foreach (var user in ConnectedUsers)
        {
            conversation.MessageUsers.Add(user);
        }

What I'm trying to understand is, when I add a 'MessageUser' to the collection property of 'Conversation', why isn't an inverse relation is established? What I mean to say is, after I add the entity to the collection, that entity's collection property should also show the object in it's collection to which is was added.

enter image description here

In the image above, the collection property of the entity which was added to 'Conversation' table has 0 'Conversation' objects. Is it supposed to be this way or am I doing something wrong?

Upvotes: 1

Views: 69

Answers (1)

Rangesh
Rangesh

Reputation: 728

Entity framework is lazy by default,how about trying to include the Conversation when you fetch the MessageUsers. i.e something like this

List<MessageUser> ConnectedUsers = db.MessageUsers.Include(x=>x.Conversation)ToList()

As you pointed out in the comment,the association/mapping is all good and that's the way EF works by default(being lazy).

Upvotes: 2

Related Questions