Split up the OnModelCreating method into smaller methods (fluent mapping)
Posted: Thu 07 Feb 2013 12:35
We just ran into a very obscure and somewhat comical issue where we were getting a StackOverflowException in the OnModelCreating method in certain cases. As it turned out it was caused by the OnModelCreating method having gotten so big that it was breaking the default IIS 32-bit stack limit of 256 KB, presumably because the calls to Entity<T> command etc. keep growing the stack. We have about 200 entities in our model, and we're clearly somewhere between the aforementioned IIS limit and the more standard .NET limit of 1 MB.
We've now modified our version of the DbContext template so each entity gets its own method, and that fixed the issue. But we'd prefer it if this was the standard behavior of your DbContext template.
On a related note, Visual Studio often struggles really badly when you try to open the generated file with the DbContext in it, presumably because it's hard for it to seamlessly compile all the fluent mapping code on the fly. Perhaps there could be an option that allowed splitting the DbContext across multiple files as well? This is a much less important point than the one about splitting OnModelCreating into smaller methods though.
We've now modified our version of the DbContext template so each entity gets its own method, and that fixed the issue. But we'd prefer it if this was the standard behavior of your DbContext template.
On a related note, Visual Studio often struggles really badly when you try to open the generated file with the DbContext in it, presumably because it's hard for it to seamlessly compile all the fluent mapping code on the fly. Perhaps there could be an option that allowed splitting the DbContext across multiple files as well? This is a much less important point than the one about splitting OnModelCreating into smaller methods though.