I'm unable to use the SetInitializer method of EF6 DbConfiguration. Here's the problem I'm encountering.
I have one project for my model, HotelRoomsModel, which inherits DbContext.
I'm using Migrations so in the same project, I have a Configuration file.
In a second project I created a DbConfiguration class and one of the configuraitons is to set the database initializer for my model.
namespace DataLayer.DbConfigurations
{
public class CustomDbConfiguration : DbConfiguration
{
public CustomDbConfiguration()
{
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
SetDatabaseInitializer(new MigrateDatabaseToLatestVersion
<HotelRoomsModel, HotelModel.HotelMigrations.Configuration>());
}
}
}
In order to specify the context, this project needs a reference to the model's project.
Now I need to "trigger" the configuration. One method as per your overview is to use an attribute in the DbContext. However, that would require the model project to have a reference to the DbConfiguration project which would create a circular reference.
So that's problem #1 because my understanding of the specs and the overview samples was that I should be able to do this.
The alternate suggestion is to specify the DbConfiguration in the app.config file.
<entityFramework codeConfigurationType="DataLayer.DbConfigurations.CustomDbConfiguration,DbConfigurations"/>
I have yet another project which is a console app. In the main method all I'm doing is initializing my model.
If I run without debugging, I get a stackoverflow exception.
If I debug I can see it hit the code as follows:
1) Instantiate HotelsRoomsModel class
2) Instantiate CustomDbConfiguration class
3) SetDefaultCOnnetionFactory in CustomDbConfiguraiton
4) SetDatabaseInitalizer in CustomerDbConfiguration
5) Then it seems to recursively instantiate this class over and over until it overflows.
I stopped it before it overflowed even then the inellitrace file I collected is over 16MB and too big to attach.
Can you easily reproduce it? Would you like me to attach a solution? Or am I doing something very obviously wrong that you can see in the description above?
Comments: EnsureLoadedForContext is called from various places as soon as a context type is known to ensure that the correct DbConfiguration is found. However, sometimes the derived DbConfiguration itself can make use of a context type (such as when the Migrations initializer is used) in which case this would cause recursive calls into EnsureLoadedForContext resulting in stack overflow. The solution is to flag that the assembly has been visited at the start of the call instead of the end.
I have one project for my model, HotelRoomsModel, which inherits DbContext.
I'm using Migrations so in the same project, I have a Configuration file.
In a second project I created a DbConfiguration class and one of the configuraitons is to set the database initializer for my model.
namespace DataLayer.DbConfigurations
{
public class CustomDbConfiguration : DbConfiguration
{
public CustomDbConfiguration()
{
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
SetDatabaseInitializer(new MigrateDatabaseToLatestVersion
<HotelRoomsModel, HotelModel.HotelMigrations.Configuration>());
}
}
}
In order to specify the context, this project needs a reference to the model's project.
Now I need to "trigger" the configuration. One method as per your overview is to use an attribute in the DbContext. However, that would require the model project to have a reference to the DbConfiguration project which would create a circular reference.
So that's problem #1 because my understanding of the specs and the overview samples was that I should be able to do this.
The alternate suggestion is to specify the DbConfiguration in the app.config file.
<entityFramework codeConfigurationType="DataLayer.DbConfigurations.CustomDbConfiguration,DbConfigurations"/>
I have yet another project which is a console app. In the main method all I'm doing is initializing my model.
If I run without debugging, I get a stackoverflow exception.
If I debug I can see it hit the code as follows:
1) Instantiate HotelsRoomsModel class
2) Instantiate CustomDbConfiguration class
3) SetDefaultCOnnetionFactory in CustomDbConfiguraiton
4) SetDatabaseInitalizer in CustomerDbConfiguration
5) Then it seems to recursively instantiate this class over and over until it overflows.
I stopped it before it overflowed even then the inellitrace file I collected is over 16MB and too big to attach.
Can you easily reproduce it? Would you like me to attach a solution? Or am I doing something very obviously wrong that you can see in the description above?
Comments: EnsureLoadedForContext is called from various places as soon as a context type is known to ensure that the correct DbConfiguration is found. However, sometimes the derived DbConfiguration itself can make use of a context type (such as when the Migrations initializer is used) in which case this would cause recursive calls into EnsureLoadedForContext resulting in stack overflow. The solution is to flag that the assembly has been visited at the start of the call instead of the end.