Jak włączyć obiekt potomny do struktury encji 5

Używam Entity Framework 5 code first i ASP.NET MVC 3.

Walczę o to, aby obiekt dziecka wypełnił się. Poniżej moje zajęcia..

Klasa aplikacji;

public class Application
{
     // Partial list of properties

     public virtual ICollection<Child> Children { get; set; }
}

Klasa dziecka:

public class Child
{
     // Partial list of properties

     public int ChildRelationshipTypeId { get; set; }

     public virtual ChildRelationshipType ChildRelationshipType { get; set; }
}

Klasa Child:

public class ChildRelationshipType
{
     public int Id { get; set; }

     public string Name { get; set; }
}

Część metody GetAll w repozytorium zwracająca wszystkie aplikacje:

return DatabaseContext.Applications
     .Include("Children");

Klasa potomna zawiera odniesienie do klasy ChildRelationshipType. Do pracy z dziećmi aplikacji miałbym coś takiego:

foreach (Child child in application.Children)
{
     string childName = child.ChildRelationshipType.Name;
}

Pojawia się błąd, że kontekst obiektu jest już zamknięty.

Jak określić, że każdy obiekt potomny musi zawierać obiekt ChildRelationshipType, tak jak to zrobiłem powyżej?

Author: jdmcnair, 2012-10-24

4 answers

Jeśli dołączysz bibliotekę System.Data.Entity możesz użyć przeciążenia metody Include(), która pobiera wyrażenie lambda zamiast ciągu znaków. Można wtedy Select() nad dziećmi z wyrażeniami Linq zamiast ścieżek string.

return DatabaseContext.Applications
     .Include(a => a.Children.Select(c => c.ChildRelationshipType));
 270
Author: Ryan Amies,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-21 22:17:38

Z EF Core w. NET Core możesz użyć słowa kluczowego ThenInclude:

return DatabaseContext.Applications
 .Include(a => a.Children).ThenInclude(c => c.ChildRelationshipType);

Dołącz dzieci z kolekcji dziecięcej:

return DatabaseContext.Applications
 .Include(a => a.Childrens).ThenInclude(cs => cs.ChildRelationshipType1)
 .Include(a => a.Childrens).ThenInclude(cs => cs.ChildRelationshipType2);
 113
Author: Hayha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-15 23:07:24

Skończyłem robiąc następujące i działa:

return DatabaseContext.Applications
     .Include("Children.ChildRelationshipType");
 23
Author: Brendan Vogt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-20 11:45:47

Dobry przykład użycia generycznego wzorca repozytorium i implementacji generycznego rozwiązania może wyglądać mniej więcej tak.

public IList<TEntity> Get<TParamater>(IList<Expression<Func<TEntity, TParamater>>> includeProperties)

{

    foreach (var include in includeProperties)
     {

        query = query.Include(include);
     }

        return query.ToList();
}
 2
Author: gcoleman0828,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-08 17:52:19