-
-
Notifications
You must be signed in to change notification settings - Fork 594
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
BulkInsert does not populate foreign keys for child entities #95
Comments
For FKs in BULK you need FKId Properties in Parent class: public class Parent
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long ParentID { get; set; }
//[ForeignKey("ChildAID")]
public ChildA ChildA { get; set; }
public long ChildAID { get; set; }
//[ForeignKey("ChildBID")]
public ChildB ChildB { get; set; }
public long? ChildBID { get; set; }
} And you need to set them as well. new Parent
{
ChildA = child,
ChildB = child.ChildB,
ChildAID = child.ChildAID,
ChildBID = child.ChildBID
} Also if you don't need to use Navigation Properties you don't have to set them, you could just have: new Parent
{
ChildAID = child.ChildAID,
ChildBID = child.ChildBID
} |
Is there no way to make it work without exposing the IDs? I don't actually need any of the IDs but I do need the navigation properties in my model. |
In that case, keep Navigation but add IDs also to Entity. I find it good practice to always have explicit IDs. Anyway in the library column is mapped with the Property and Navigation props. are ignored. |
I have an set of POCOs that are set up like this:
My problem is that when using BulkInsert on Parent entities that have existing ChildA and ChildB entities, the keys for ChildA and ChildB are not populated on the Parent's table. My code does something like this:
This does insert to the Parent table, but both ChildAID and ChildBID is null. Replacing the BulkInsert call with the code below populates both child IDs properly but is obviously slow.
The text was updated successfully, but these errors were encountered: