-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More than one ForeignKeyAttribute pointing to same property causes stackoverflow #3799
Comments
Hey, You shouldn't really be disposing something that you get from DI (since DI will take care of disposing it for you). Also, you really need to get the context from a scoped set of services, not the root one. Confusing I know... such is the nature of DI 😄. Can you try swapping to the following code and see if things work? using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
var db = serviceScope.ServiceProvider.GetService<MyDbContext>();
// whatever you want to do with the context
} |
Thank you, the scope actually does make more sense now ( although confusing at first :) ). Regarding the test, it doesn't help I'm afraid, I still have the StackOverflowException ( I have edited the initial post to show the call stack. |
Did you have existing migrations that you hand edited to still compile on RC1? If so, could you try removing those and regenerating? I wonder if the old migrations are hitting a code path that should not be hit. |
Yes I did and on your suggestion I removed all my migrations directory and tried to regenerate a migration via dnx ... Dnx crashed with StackOverflowException.. nb1: None of my models changed while upgrading to rc1 and it was working until today. |
@rowanmiller , I think there is a good chance my issue could be the same as issue #3806 ( One-to-one relationship with navigation properties on both ends throws StackOverflowException ) I don't know if it is related to one-to-one navigation property in particular, but I have lots of models with lots of navigation properties in them :S |
Bingo ! it was indeed related to navigation properties. public class ModelA
{
public virtual ICollection<ModelB> Bs { get; set; }
public virtual ICollection<ModelB> Bs_1 { get; set; }
}
public class ModelB
{
public virtual ModelA A { get; set; }
public virtual ModelA A_1 { get; set; }
} In beta7/beta8 this behaviour seemed to be tolerated and have no impact. Hope this helps someone and thanks for having a look at it anyway Rowan. |
Make sure to report this bug o the reverse poco codeplex site |
Re-opening... even if the code is incorrect you should not get a StackOverflow |
@julienbjuice - Can you share full model and your model configuration? The model you have posted would not generated any issues since there are 2 navigations at both ends and it is ambiguous how to create relationship so EF will not do anything by convention. |
Sorry for the delay, @smitpatel, I simplified my models as much as I could. My full database has more than 100 tables so it was less than ideal to share here. The Exception is easy to reproduce with the following models. To remove the exception, just comment out the code between the tags [STACKOVERFLOWEXCEPTION SECTION] public class Model_A
{
public Guid Other_Id { get; set; }
[ForeignKey("Other_Id")]
public virtual Model_B model_b { get; set; }
// [STACKOVERFLOWEXCEPTION SECTION]
[ForeignKey("Other_Id")]
public virtual Model_B model_b_1 { get; set; }
// [/STACKOVERFLOWEXCEPTION SECTION]
}
public class Model_B
{
public Model_B()
{
model_a = new HashSet<Model_A>();
// [STACKOVERFLOWEXCEPTION SECTION]
model_a_1 = new HashSet<Model_A>();
// [/STACKOVERFLOWEXCEPTION SECTION]
}
[InverseProperty("model_b")]
public virtual ICollection<Model_A> model_a { get; set; }
// [STACKOVERFLOWEXCEPTION SECTION]
[InverseProperty("model_b")]
public virtual ICollection<Model_A> model_a_1 { get; set; }
// [/STACKOVERFLOWEXCEPTION SECTION]
} @ErikEJ, the poco reverse project was not to blame on this. The original sql script used to generate the database was flawed. The tool did what it had to do. Hope this helps, |
@julienbjuice - Thanks for the model details. As you can see the attribute configuration is ambiguous for model creation. For |
Hi,
I have just upgraded my project to RC1 and have now a few issues with my DbContext setup.
I actually have two issues that are described in the code below :
For more information, the ObjectDisposedException gives me the following call stack :
[Edit]
Regarding the StackOverflowException, this might be interesting (or not)
Also, it is worth noting that I had to convert my migration files to match the new naming convention ( Annotation() => HasAnnotation(), Index() => HasIndex(), Unique() => IsUnique() )
What am I missing ?
The text was updated successfully, but these errors were encountered: