How to Insert a multi-nested item using C#/Dapper?

I have a series of classes that look like this:

public class SignUpList {
    public int? SignUpListId { get; set; }
    public IEnumerable<SignUpItem> Items { get; set; }
}

public class SignUpItem {
    public int? SignUpItemId { get; set; }
    public int? SignUpListId { get; set; }
    public IEnumerable<ItemPerson> Persons { get; set; }
}

public class ItemPerson {
    public int? ItemPersonId { get; set; }
    public int? SignUpItemId { get; set; }
    public int PersonId { get; set; } //fk reference to another table
}

(Think of a scenario like Sign Up Genius: I have a list created called “Classroom Party”. On the list I have “Plates”, “Forks”, and “Napkins”. Then 4 people can sign up for each).

Now, when my API endpoint that uses the SignUpList is hit, I’m happy to use some UDT’s as parameters and merge all my records into my existing tables.

I’m having an issue with the logic for how to insert a record for the first time when I don’t already have an ItemPersonId on the ItemPerson.

For example:

I can save my SignUpList and get back the SignUpListId using something like this with Dapper:

    var listId = ExecuteScalar<int>("dbo.SaveList", parameters, commandType: StoredProcedure) 

And then I can populate that into a UDT for inserting the SignUpLists (or even insert those in the same query). My issue comes from the next layer of nesting:

How do I get a SignUpItemId to put onto my persons? I can’t just shove them in a UDT because I’ll have no way to know which SignUpItem the person ties back to.

I could do some foreach looping shenanigans –

    foreach (var item in SignUpList.Item) {
        var itemId = ExecuteScalar("[dbo].[SaveItem]", parameters); //assume I've created appropriate parameters
        //Insert into my DataTable based off the UDT for ItemPerson
    }
    _ = ExecuteAsync("dbo.SavePersons", params) //(params being UDT table populated in the foreach above)

But that approach is costly as far as database operations go.

Another option I’ve considered is a mass insert of ALL the SignUpItems in one go, but then how do I tie those ID’s back to my existing list? (I don’t think I can make a new list of SignUpItem based on the return because it won’t have the Person tied to the items, which is the whole issue).

I also considered using a “row number” and using that as a pseudoId to link them. It would require adding a new field to the parent item but may be the appropriate play if I’m trying to minimize db connections?

Is there something really simple I’m missing in Dapper that can handle this more effectively?

Unfortunately, it’s not a viable option to require saving the List vs Items separately due to the nature of the application, otherwise that would be my preferred approach.

If a different data structure would be better, I’m also willing to consider it, but I struggle to envision a way to make this logic work without triple nesting. I also considered doing ItemPerson as a serialized JSON field on the SignUpItem but I need to be able to query from it as well.

In your case I suggest following:

  1. Define 3 user-defined table types, corresponding your entity types
  2. Create procedure taking three parameters, of above mentioned types
  3. Flatten your data to 3 collection with no nesting and pass them to before mentioned procedure in single procedure execution
  4. Now, to resolve linking extend both your db type and C# classes with artificial IDs that would simulate parent-child link/relation of the entities
  5. Assign them in some sort of loop
  6. In your stored procedures you’ll be able to correlate appropriate records

I used such approach few times and it always worked for me

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật