I have an N-N relationship between two elements Sfera and Contenuti.
On the Sfera item I see the Contenuti entity set and in the Contenuti I see the Sfera set..
When I add a new item by code i use a structure like:
using (IndexDB DB = new IndexDB())
{
try
{
var newContenuto = new Contenuto();
newContenuto.Cancellato = false;
newContenuto.PK_Content_ID = tt_content_id;
newContenuto.URL = URL;
foreach(long sphere in SphereID)
{
try
{
var sfere = from sfera in DB.Sfera where sfera.PK_Sfera == sphere select sfera;
newContenuto.Sfera.Add(sfere.First());
sfere.First().Contenuto.Add(newContenuto);
}
catch (Exception exc)
{
return new StandardResponse() {Success = false, Message = exc.Message};
}
}
DB.AddToContenuto(newContenuto);
DB.SaveChanges();
return new StandardResponse() {Success = true};
}
catch (Exception e)
{
return new StandardResponse() { Success = false, Message = e.Message + e.StackTrace };
}
If I take a look on my DB, it stores well my relation between the two elements in the right “N-N” table…. But when i try to access the elements Contenuto.Sfera and Sfera.Contenuto are ALWAYS empty set…
To access I do something like that:
using (IndexDB DB = new IndexDB())
{
var sfere = from sfera in DB.Sfera where sfera.PK_Sfera == IDSfera && sfera.Attiva select sfera;
if (!sfere.Any())
{
response.Add(new UrlResponse() { Success = false, ErrorMessage = "" });
}
var sferaSelezionata = sfere.First();
//HERE sferaSelezionata.Contenuto.Count == 0 even if on DB there are MANY "connections"
}
}
How can I handle this?
Thank you very much!
Try using
Include()in your query.