I'm working on a messaging platform type sales platform. A buyer can contact a seller about a product. Their message concerns a specific product.
The exchange of messages is only possible between a buyer and the owner of the product, and will only concern the product in question.
The exchanges are recorded in a database.
This is the model - columns User_Exp, User_Dest and Article are foreign keys
public class MessagesModel
{
public long Id { get; set; }
public ApplicationUser User_Exp { get; set; }
public ApplicationUser User_Dest { get; set; }
public Articles Article { get; set; }
public string Message { get; set; }
public messageStatus Status { get; set; }
public DateTime Created_at { get; set; }
public enum messageStatus
{
Sent,
Delivered
}
public MessagesModel()
{
Status = messageStatus.Sent;
}
}
When loading the "Messages" view, I retrieve the exchange history:
function getChat ( ) {
$.ajax ( {
url: '@Url.Action("ConversationWithContact", "Messaging")' ,
type: 'GET' ,
cache: false ,
data: { articleId: '@Html.DisplayTextFor(model => model.Id)' } ,
} )
.done ( function ( resp ) {
var chat_data = resp.data || [ ] ;
loadChat ( chat_data ) ;
} ) ;
} ;
The controller code:
public JsonResult ConversationWithContact(long articleId)
{
ApplicationUser currentUser = _repo.GetUser();
Articles item = _repo.GetArticleByID(articleId);
var conversations = new List<Models.Messages.MessagesModel> ( ) ;
conversations = _context.Messages
.Where(c => (c.Article.Id == item.Id)
&& (c.User_Dest.Id == currentUser.Id ||
c.User_Exp.Id == currentUser.Id))
.OrderBy(c => c.Created_at)
.ToList();
return Json (new { status = "success", data = conversations });
}
This is where the problem shows up.
For the example I have 2 users, Titi and Toto, and a salesperson. Titi and Toto each sent a message to the salesperson about the same product.
conversations returns NULL for User_Exp while the value is present in the database.
The problem occurs in the controller:
This happens for every message for which the user using the messaging is not the sender.
When it is the RECIPIENT (in this case the product owner) who uses the messaging, each User_Exp is NULL:
For Titi, it is Toto's messages that have user_Exp = NULL:
And for Toto, it is Titi's messages:
I absolutely need the user_Exp info to then sort the messages.
I can't understand this problem. Maybe it's just a logic problem, but I don't understand it.
Thanks




Select()or a mapping provider like AutoMapper. This negates worrying about whether data is eager loaded or not, and helps produce far more efficient queries..Include(c => c.User_Exp)to your query [in the controller])_context.Messages.Include(c => c.User_Exp); _context.Messages.Include(c => c.User_Dest); conversations = _context.Messages. Where(c => (c.Article.Id == item.Id) && (c.User_Dest.Id == currentUser.Id || c.User_Exp.Id == currentUser.Id)) .OrderBy(c => c.Created_at) .ToList();.Include()needs to be part of the query. calling_context.Messages.Include(...)on its own won't do anything. I put the updated query expression that should give you the data you expect in an answer below.