Why don't my child table entities show up when i select objects using LINQ ORM mapping
I am trying to construct an object containing several listings. The object - a message - is represented by one table and the contained lists represented by one table each.
The problem is that when selecting messages, the properties supposed to reference the contents in the child tables all have a zero count even if there are values in the child tables.
I have simplified the database so that i only have one associated child table to provide a good example of the problem.
What I have:
One table called Messages that contains the following properties:
- Id - uint - autogenerated - primary key
- Message - string
One table called Recipients that contains the following properties:
- MessageId - uint - primary key
- RecipientId - uint - primary key
One association between these two. Messages is parent, recipients is child. Cardinality is one-to-many. Participating properties are Messages.Id -> Recipients.MessageId.
My objective is to be able to save messages. Each message should have one or more recipients. When i extract a message from the Messages table, i want the Recipients property (generated from the association to the Recipients table) to give me back all of the recipients where the MessageId equals the Id of the extracted message.
So, I insert into the database:
void SendMessage(string message, uint recipient)
{
var m = new Message { Message = message };
context.Messages.InsertOnSubmit(m);
context.SubmitChanges();
var r = new Recipient { MessageId = m.Id, RecipientId = recipient };
context.Recipients.InsertOnSubmit(r);
context.SubmitChanges();
}
Now, if i select the messages from the Messages table, i find the message. Its Recipients property has Count = 0. Somehow the association to recipients is not made. So I must do something wrong.
However - and this really confuses me - if i select the recipients from the recipient table, i find the recipient with the correct MessageId and RecipientId. The ORM framework has also generated a property that references the parent table and this one works! The Recipient entity's Message property actually references the Message that is its parent.
Can anyone give me a hint on what I'm missing in order to be able to list all the child entities of a parent entity? Or on what more information one might n开发者_开发百科eed to give me an answer?
private void SendMessage(string message, uint recipient)
{
var message = new Message { Message = message };
//no need to explicit the relation with message
var r = new Recipient { RecipientId = recipient };
//add the recipient to message's recipients
message.Recipients.Add(r);
context.Messages.InsertOnSubmit(message);
//only one submit is needed
context.SubmitChanges();
}
As you found out by yourself, the auto-generated Message
class has a Recipients
property of type EntitySet<Recipient>
, which is basically all the recipients associated to that message.
If you add the recipient to the message you are going to save, LINQ will automatically associate it with the proper id key when submitting the changes.
精彩评论