Get data from a many-to-many relationship with EF and web-api

0

A simple question but I do not give. I have a many to many relationship between two Citizen entities and Topics (which is generated from the Citizen, Topics and CitizensTopics tables)

And I have a Web-Api service from which I want to obtain the Topics to which a citizen is subscribed. For this I use a method (which does not work)

public IQueryable<Topic> GetTopics(string Ciudadano)
{
      db.Configuration.LazyLoadingEnabled = false;
      Ciudadano c = db.Ciudadanos.Include("RTopics").FirstOrDefault(p => p.Id == Ciudadano);
      return c.RTopics.AsQueryable();

//      return db.Topics.Where(p => p.eliminado == false).OrderBy(o => o.nombreTopic); //ESTO SI FUNCIONA
}

This returns an error 500, although in RTopics is the list of Topics to which the citizen in question subscribes.

What's wrong?

Thank you very much

    
asked by Juanjo 13.11.2017 в 17:39
source

1 answer

0

In addition to disabling the lazy load you should also do it%% of%

Entity Framework Working with Proxies

using the line

db.Configuration.ProxyCreationEnabled = false; 

maybe something like being

public List<Topic> GetTopics(string Ciudadano) 
{ 
    List<Topic> result = null;
    try
    {
        db.Configuration.LazyLoadingEnabled = false; 
        db.Configuration.ProxyCreationEnabled = false; 

        Ciudadano c = db.Ciudadanos
                            .Include("RTopics")
                            .FirstOrDefault(p => p.Id == Ciudadano); 

        result = c.RTopics.ToList();
    }
    catch(Exception ex)
    {
        string e = ex.Message;
    }

    return result;
}  

you will also see that it is advisable to return a proxy and not a List<> , since a list will already have resolved the execution of the sql, perhaps this does not affect the proxy or the lazy load

Disabling the proxy and lazy load colocalor soil in the context of EF

    
answered by 13.11.2017 в 19:50