La inserción en la tabla de muchos a muchos no está visible en EntityRef. ¿Qué podría estar mal?

I have a strange Problem with LINQ-To-SQL:

I have 3 tables - Feeds, FeedTags and Tags - where FeedTags is a m:n table for Feeds and Tags.

What I'm doing is inserting a new record into Feeds and then inserting a new record into FeedTags for this new Feed.

My problem is that, even if I query the Feed object fresh from the DB, the Feed.FeedTags property, which is the EntityRef to FeedTags, is empty (0 records):

// A bit simplified, adding Feed and Tag happens in different functions, but you get the idea
context.Feeds.InsertOnSubmit(newFeed);
context.SubmitChanges();
context.FeedTags.InsertOnSubmit(newTag); 
context.SubmitChanges();

var tags = context.Feeds.Single(f => f.Id == newFeed.Id).FeedTags;

tags has 0 records here. If I only query for the Feed object, it will be returned correctly, but also with 0 FeedTags records.

But when I explicitely query the FeedTags table for FeedTags with the new FeedId, I'll get the created tag back:

// Works as expected:
var feedTag = context.FeedTags.Single(ft => ft.FeedId = newFeed.Id && ft.TagId == newTag.Id);

So, obviously everything got inserted correctly, but for some reason the parent object does include the created FeedTags. What could be wrong here?

When I restart the App, everything's working fine again, and a new query to the Feeds table will also include all created FeedTags.

I have no idea where to start to look - maybe someone has experienced this before and could point me in the right direction ...

¡Muchas gracias!

preguntado el 05 de mayo de 13 a las 19:05

Probablemente newFeed.FeedTags.Add(newTag) works better. (in stead of context.FeedTags.InsertOnSubmit(newTag);) -

// A bit simplified, adding Feed and Tag happens in different functions => Does that mean different datacontexts? -

@Pleun ... no it's the same DataContext used for all operations. Edit: now as you mention it ... as it's the same DataContext ... could this be a Caching issue? But I tried to work around that by querying the object again instead of re-using the existing Entity ... -

@GertArnold Tried that, had unfortunately the same effect -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.