You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have entities TreeContent and TreePoint that are to be in a one-to-one relationship, in simplified form:
public abstract class TreeContent
{
int CGPK { get; set; } // primary key, not called "Id"
public TreePoint? TreePoint {
get => this._treePoint;
set => this._treePoint = value;
}
private TreePoint? _treePoint;
}
public class TreePoint {
int CGPK { get; set; } // primary key
public TreeContent TreeContent {
get => this._treeContent ?? throw new InvalidOperationException();
set => this._treeContent = value;
}
private TreeContent? _treeContent;
}
It is the TreePoint that is the principle entity and the TreeContent that is the dependent (the nullability is opposite this, I know, but that is due to other reasons. I'm also using a nonstandard name for the primary key.)
I then configure the one-to-one relationship using modelBuilder.Entity<TreePoint>().HasOne( tp => tp.TreeContent).WithOne( tc => tc.TreePoint).HasForeignKey<TreeContent>( nameof(TreePoint)+"CGPK");
At run time I inspect the model and find the foreign key. It has DependentToPrincipal set correctly to be the treeContent.TreePoint property. However, the PrincipleToDependent property is null. (The generated model also does not contain a foreign key in the TreePoint referring back to the TreeContent.)
What am I missing?? I don't see how to configure the property of the principle entity that refers back to the dependent entity. I need PrincipalToDependent in the foreign key to address some validation requirements.
EF Core version
9.0.1
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
No response
Operating system
No response
IDE
No response
The text was updated successfully, but these errors were encountered:
Question
I have entities TreeContent and TreePoint that are to be in a one-to-one relationship, in simplified form:
It is the TreePoint that is the principle entity and the TreeContent that is the dependent (the nullability is opposite this, I know, but that is due to other reasons. I'm also using a nonstandard name for the primary key.)
I then configure the one-to-one relationship using
modelBuilder.Entity<TreePoint>().HasOne( tp => tp.TreeContent).WithOne( tc => tc.TreePoint).HasForeignKey<TreeContent>( nameof(TreePoint)+"CGPK");
At run time I inspect the model and find the foreign key. It has DependentToPrincipal set correctly to be the treeContent.TreePoint property. However, the PrincipleToDependent property is null. (The generated model also does not contain a foreign key in the TreePoint referring back to the TreeContent.)
What am I missing?? I don't see how to configure the property of the principle entity that refers back to the dependent entity. I need PrincipalToDependent in the foreign key to address some validation requirements.
EF Core version
9.0.1
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
No response
Operating system
No response
IDE
No response
The text was updated successfully, but these errors were encountered: