sql – How do I add child of child when dependent on parent

I’m building a wpf application which interacts with a sql database.

I’m confused about adding children of children in my window.

For example.. I just created a fishing trip so my fishing trip doesnt have a primary key yet.

I add fishes in the window which when saved will get assigned the primary key of the fishing trip.

But I also want to add users to each of the fishes.

When I create these users (many to one relationship with fishes), i can’t assign them the primary key of the fishes because I have not saved to database because I don’t have a fishing trip primary key.

So when I save, how do I go down the hierarchy past the first level?

4

When you save, just do things in the right order and wrap the whole lot in a Transaction.

  • Begin the Transaction.
  • Insert the “Fishing Trip” record. Retrieve the Primary Key for the record just inserted.
  • For each User:
    • Create each “Trip-User” record. Use the Trip’s Primary Key.
    • Retrieve the Primary Key for the “Trip-User” record just added.
    • For each User:
      • Create the “Trip-User-Fish” record(s). Use the Primary Keys from the Trip and the Trip-User records.
  • Commit the Transaction.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *