1

I have a schema like this:

* Customers
     int ID
     string Name
     List<Location> Locations

* Locations:
    int ID
    string Address
    string City
    string State
    string Zip
    int CustomerID

So I have a many-to-one relationship between "Locations" and "Customers".

I was hoping that EF would do all the "heavy lifting" for me. I'm using EF navigation, I 'm successfully able to query the "complex record with nested fields" and display it in a Razor page.

The only thing I'm NOT able to do so far is to successfully SAVE my updates to the "nested items". I can SEE my changes in the debugger in OnPost() (at least I see them when I define a separate variable/separate model binding for "Locations"). But I can't figure out how to REPLACE the old values in "Customer.Locations" with the new values in "Locations".

Or better, eliminate the "Locations" variable altogether, and just let the Razor page update the rows and columns in "Customer.Locations" directly.

This link seems relevant: Removing all records from navigation properties in Entity Framework

These are the details of what I've tried so far,and where I'm stuck: ASP.Net Core: How do I update (change/add/remove) nested item objects (One-to-Many relationship)?

Q: Any suggestions? In particular, can anybody point me to any examples? I've searched, to no avail.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • 2
    Check this function [MergeCollections](https://stackoverflow.com/a/68357569/10646316) – Svyatoslav Danyliv Aug 26 '22 at 01:56
  • Sounds like what I need: "You have to instruct ChangeTracker which operations are needed for updating navigation collection." I'll try it and let you know - thank you! – FoggyDay Aug 26 '22 at 01:59

0 Answers0