Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: DbConfiguration.SetInitializer Reference Context Problem [671]

$
0
0
I'm unable to use the SetInitializer method of EF6 DbConfiguration. Here's the problem I'm encountering.<br /><br />I have one project for my model, HotelRoomsModel, which inherits DbContext.<br />I'm using Migrations so in the same project, I have a Configuration file.<br /><br />In a second project I created a DbConfiguration class and one of the configuraitons is to set the database initializer for my model.<br /><br />namespace DataLayer.DbConfigurations<br />{<br /> public class CustomDbConfiguration : DbConfiguration<br /> {<br /> public CustomDbConfiguration()<br /> {<br /> SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));<br /> SetDatabaseInitializer(new MigrateDatabaseToLatestVersion<br /> <HotelRoomsModel, HotelModel.HotelMigrations.Configuration>());<br /> }<br /> }<br />}<br /><br />In order to specify the context, this project needs a reference to the model's project.<br /><br />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.<br /><br />So that's problem #1 because my understanding of the specs and the overview samples was that I should be able to do this.<br /><br />The alternate suggestion is to specify the DbConfiguration in the app.config file. <br /><br /> <entityFramework codeConfigurationType="DataLayer.DbConfigurations.CustomDbConfiguration,DbConfigurations"/><br /><br />I have yet another project which is a console app. In the main method all I'm doing is initializing my model.<br /><br />If I run without debugging, I get a stackoverflow exception.<br /><br />If I debug I can see it hit the code as follows:<br /><br />1) Instantiate HotelsRoomsModel class<br />2) Instantiate CustomDbConfiguration class<br />3) SetDefaultCOnnetionFactory in CustomDbConfiguraiton<br />4) SetDatabaseInitalizer in CustomerDbConfiguration<br />5) Then it seems to recursively instantiate this class over and over until it overflows.<br /><br />I stopped it before it overflowed even then the inellitrace file I collected is over 16MB and too big to attach.<br /><br />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?<br /><br />

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>