문제

I'm trying to apply the Aggregate Root pattern to my domain. I'm using Entity Framework 4 with POCO Entity Generator.

I have two entities: MailingTask EmailLog With a one-to-many relationship. (MailingTask has many EmailLogs). EF4 generates a navigation property on MailingTask:

public virtual ICollection<EmailLog> EmailLogs

Within my model I never want to access EmailLogs directly, always through its parent MailingTask. This is enforced by:

  • Having only a MailingTask Repository, so I cannot query the EmailLogs table directly.
  • Getting EmailLogs through this navigation property only.

I sometimes need to count the number of EmailLogs for a MailingTask. This is achieved by using LINQ on the navigation property:

mailingTask.EmailLogs.Count();

But this executes on the application-side (not database server). (And it is very expensive since I have a lot of EmailLogs for a MailingTask.) I read a few posts about this behavior and it seems to be that EF4 navigation properties cannot be used as IQueryable (executed on database-side). When you access a navigation property, EF4 loads ALL entries with ALL columns in memory and applies the LINQ expression in memory. For a Count() statement, this hurts a lot.

I believe I will have to change my model to accommodate this specialized querying (possibly adding an EmailLogsRepository with querying abilities). For me, it seems like EF4 does not support the Aggregate Root pattern very nicely. Or maybe I'm missing something about the Aggregate Root pattern and how it should be implemented with regards to EF4...

Did anyone encounter this situation and was able to solve this? Does nHibernate or another ORM support this better?

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top