-
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
Throw for ambiguous self-referencing navigations #13573
Comments
Share your model configuration. |
Note that this was also posted on Stack Overflow and David was unable to reproduce the issue. @parxal Based on the responses from David and Smit, can you please provide a runnable project/solution or complete code listing that demonstrates the behavior you are seeing? |
@ajcvickers just upload my example https://github.com/parxal/EntityFrameworkCore , @smitpatel check my user model class |
@parxal Thanks! Note for triage: the model created is making CreatedBy and UpdatedBy inverses of each other:
@parxal We will discuss if this is the correct model to build by convention. You should be able to fix your issue by explicitly configuring the two relationships as separate. For example: modelBuilder
.Entity<User>()
.HasOne(e => e.CreatedBy)
.WithMany()
.OnDelete(DeleteBehavior.ClientSetNull); // To prevent cascade cycles on SQL Server
modelBuilder
.Entity<User>()
.HasOne(e => e.UpdatedBy)
.WithMany(); |
@ajcvickers thanks, i just fixed it with: modelBuilder.Entity().HasOne(e => e.CreatedBy).WithMany(); |
2 navigations between a pair of entity (same entity in this case), are always paired of if it is not ambiguous. If you want to declare multiple navigations, you would need to manually configure them. |
@parxal "Should this line be Required when creating the model?" is something we need to discuss. |
I would think that unless CreatedById is unique, that UpdatedBy cannot be the inverse navigation property of CreatedBy. |
@davidbaxterbrowne My guess is we currently pivot on matching navigations before attempting to look for FKs, and hence CreatedById is not even matched as an FK. |
Notes from triage: This: public class User
{
public Guid Id { get; set; }
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
public Guid CreatedById { get; set; }
public Guid? UpdatedById { get; set; }
} is essentially the same with a self-referencing relationships as this is without them: public class Bar
{
public Guid Id { get; set; }
public Foo UpdatedBy { get; set; }
public Guid? UpdatedById { get; set; }
}
public class Foo
{
public Guid Id { get; set; }
public Bar CreatedBy { get; set; }
public Guid CreatedById { get; set; }
} This throws for ambiguity, and so should the self-referencing example. |
Poaching... |
Verified that this is already fixed in 3.0. (Also verified that it did throw in 2.2, so this is a 3.0 fix.) Issue #13573
Specifically: * New note for dotnet/efcore#13573 * First attempt at a summary--feedback appreciated. Not sure if a table is right here?
Verified that this is already fixed in 3.0. (Also verified that it did throw in 2.2, so this is a 3.0 fix.) Issue #13573
Specifically: * New note for dotnet/efcore#13573 * First attempt at a summary--feedback appreciated. Not sure if a table is right here?
* Updates to breaking-changes.md Specifically: * New note for dotnet/efcore#13573 * Add more usable summary
Include Method are Generating Wrong Sql Join
Steps to reproduce
SQL
The same instructions in Entity Framework 6.2 works fine. (this code only returns 3 records)
In entityframeworkcore sql generate join is "strange", and returns 4 records.
Further technical details
EF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system:
IDE: Visual Studio 2017 15.8.5
The text was updated successfully, but these errors were encountered: