Share via

OData - How to implement @odata.bind to insert Foreign key value to a related table in .NET Core 6 using C#?

Donald Gonzales 0 Reputation points
2023-07-25T18:47:52.11+00:00

Hello, in Postman I can POST request successfully with this raw data:

{
       "******@odata.bind": "Contacts(3b36c706-6a6c-414f-8f5f-ed24fc9a5aa2)",
       "stringName": "Shrek Rattle"
}

This will insert "3b36c706-6a6c-414f-8f5f-ed24fc9a5aa2" in the relatedContact column of PreferenceManagement table. Contact and PreferenceManagement tables has a one-to-many relationship via ContactId from Contact table and relatedContact in PreferenceManagement table. This is working fine when trying in Postman, and I'm able to see the posted records in PreferenceManagement table in the database.

My problem is, HOW can I apply this same functionality in .NET Core 6 using C#? From Swagger/UI, I tried sending that same Postman request but no success!

Note: When calling PostAsync for table that doesn't have foreign key values to be inserted, it's also working just fine. For example, if I just put {"stringName": "Shrek Rattle"} in Swagger/UI and click the Post button, I'm not getting any issue in my code and the data is Posted in the PreferenceManagement, but with a null relatedContact value. But I need that relatedContact value as it will relates the Contact records to the Preference records.

Any thoughts? and thanks in advance for any help, suggestions, and recommendations!!

Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.