Ищете лучший способ использования «.Include» в EFC#

Место общения программистов C#
Anonymous
Ищете лучший способ использования «.Include» в EF

Сообщение Anonymous »

Я ищу лучший способ использования предложения .Include Entity Framework. Я хочу избежать дублирования кода.
У меня много классов, и у каждого класса есть метод, который выглядит примерно так:

Код: Выделить всё

query = (from n in currentDBContext.FBBuchungenCollection
.Include(x => x.BelegHerkunft)
.Include(x => x.Buchungsordner)
.Include(x => x.Buchungsperiode).ThenInclude(x => x.Geschaeftsjahr)
.Include(x => x.BuchungsUser)
.Include(x => x.Erfassungsart)
.Include(x => x.ErstellUser)
.Include(x => x.Mandant).ThenInclude(x => x.HauptAdresse)
.Include(x => x.StornoUser)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.FremdWaehrung)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.KKArt)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.KKKonto)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.Konto)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.KoReVerteilung).ThenInclude(x => x.Periodenverteilungen).ThenInclude(x => x.Kontierungen).ThenInclude(x => x.Kontierungsangaben).ThenInclude(x => x.KontierungsangabenKTR).ThenInclude(x => x.Kostentraeger)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.KoReVerteilung).ThenInclude(x => x.Periodenverteilungen).ThenInclude(x => x.Kontierungen).ThenInclude(x => x.Kontierungsangaben).ThenInclude(x => x.KontierungsangabenKTR).ThenInclude(x => x.KostentraegerArt)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.OffenerPosten).ThenInclude(x => x.Kostenstelle)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.OffenerPosten).ThenInclude(x => x.Kostentraeger)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.OffenerPosten).ThenInclude(x => x.Mandant)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.OffenerPosten).ThenInclude(x => x.Sachkonto)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.Steuerschluessel)
.Include(x => x.Teilbuchungen).ThenInclude(x => x.VerweisTeilbuchung)
select n);
Пример другого класса:

Код: Выделить всё

query = (from n in currentDBContext.KKKontoCollection
.TagWith("KKKonto.BuildQuery")
.Include(x => x.Mandant).ThenInclude(x => x.HauptAdresse)
.Include(x => x.KKArt)
.Include(x => x.Land)
.Include(x => x.Mahnkategorie)
.Include(x => x.SammelKonto)
.Include(x => x.Zahlungsbedingung)
select n);
Вы можете видеть, что некоторые включения равны, в данном случае строка:

Код: Выделить всё

.Include(x => x.Mandant).ThenInclude(x => x.HauptAdresse)
Есть ли способ избежать этого? Что мне нужно, так это своего рода AddIncludeMandant(), который я могу использовать в каждом классе, которому это нужно?
Привет, Карстен

Подробнее здесь: https://stackoverflow.com/questions/798 ... lude-in-ef

Вернуться в «C#»