-
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
Foreign key from root parent not set in child entity nested at third level. #28814
Comments
The relationship between |
Sorry I may sound novice but I did not get what do you mean when you say "Relationship is not being set to anything". I have set
it does not work. |
These are the relationships in the model:
This: Child child = new Child
{
Name = "Child",
Logs = new List<ChildLog>
{
childLog
}
}; relates a This: Parent parent = new Parent
{
Name = "parent",
Childs = new List<Child>
{
child
},
}; relates a Without uncommenting the code, nothing uses relationship 2. to relate a |
So now I have two part questions - If not, can we not have this feature of adding data in drill down kind of way, beacuse this approach seems not fit. We have to structure the data out of heirarchy for navigations. For e.g. if data is coming through API , we have to structure that in this way to create it? |
@shashank2490 A typical aggregate structure has references from the parent to its children, and these children to their subchildren, and so on. For example: public class Parent
{
public int Id { get; set; }
public ICollection<Level1> Level1s { get; set; }
}
public class Level1
{
public int Id { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
public ICollection<Level2> Level2s { get; set; }
}
public class Level2
{
public int Id { get; set; }
public int Level1Id { get; set; }
public Level1 Level1 { get; set; }
} Or: public class Parent
{
public int Id { get; set; }
public ICollection<Level1> Level1s { get; set; }
}
public class Level1
{
public int Id { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
public int Level2Id { get; set; }
public Level2 Level2 { get; set; }
}
public class Level2
{
public int Id { get; set; }
public ICollection<Level1> Level1s { get; set; }
} It's not common to have references directly between the parent and the subchilden--e.g. between Parent and Level2 here. You can, of course, do so, but then the semantics of that relationship will rarely be coupled with the semantics of the relationships between the parent and the first level, or the fist level and the second level. Typically if you want to navigate from Parent to Level2, then that would be |
Duplicate of #21673 This is Skip navigation traversing collection. Unlikely that we will support it (any time soon). |
Bug
When trying to set foreign key in child entity at third level in nested objects, the Root parent foreign key is not set there. Error thrown by system is "FOREIGN KEY constraint failed" . But when I add the third level child as collection in root (commented code), it works fine. So the question now is, is this the only way to do it, or there is some better way?. Ideally the way should have been the one which is not working.
Code
Stack traces
Include verbose output
The change tracker shows the following output
Include provider and version information
EF Core version: 6.0.8
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)SQLLite
Target framework: (e.g. .NET 5.0) .NET 6.0
Operating system: Windows 10
IDE: (e.g. Visual Studio 2019 16.3) Visual Studio Professional 2022 (64-bit) - Version 17.1.3
Question
So the question now is, is this the only way to do it, or there is some better way?. Ideally the way should have been the one which is not working.
The text was updated successfully, but these errors were encountered: