Asked by:
WebApi endpoint many to many relationships

Question
-
User-154227990 posted
I am developing an asp.net core web API application using an endpoint provided by our designer team. It’s many to many relationships. Below are the tables
Tables Name:
- User (UserId, Name, Gender).
- Role (RoleId, Title)
- UserRole (UserId, RoleId).
Relationship:
- User (1) to (Many) UserRole
- Role(1) to (Many(UserRole).
Role End Points:
- GET –> V1/Roles
- POST –> V1/Roles
- PUT –> V1/Roles (Update many recordstogether – JSON Array as request body)
- PUT –> V1/Roles/{RoleId}
- DELETE –> V1/Roles/{RoleId}
- DELETE V1/Roles (DELETE many recordstogether – JSON Array as request body)
User Endpoints:
- GET –> V1/Users
- POST –> V1/Users
- PUT –> V1/Users/{UserId}
- DELETE –>V1/Users/{UserId}
UserRole End points:
- GET –> V1/ UserRole
- POST –> V1/ UserRole
- PUT –> V1/ UserRole
- DELETE–> V1/ UserRole
Question: Assuming that we have thousands of users and thousands of roles in our organization. Please advise what is the best practice for designing endpoint for many to many relationships’ tables.
Also, if we want to add, update or delete many users and their assigned roles in one call what is the best way?
I have created the below endpoint for joining tables but not sure. please advice.
- POST –> V1/ UserRole
- PUT –> V1/ UserRole
- DELETE –> V1/ UserRole
How to create endpoint for many to many relationships
Sunday, May 30, 2021 11:12 PM
All replies
-
User-1545767719 posted
Are you trying to develop the authentication and authorization system of ASP.NET Core web application?
If yes I suggest that you use ASP.NET Core Identity rather than developing your own authentication mechanism.
The database will automatically be created by ASP.NET Core Identity using entity framework code first. You can add your own personal data such like "Gender" shown in your question above to the created database later.
Even though you have to use your own tables for some reason I suggest that you develop only the Data Access Layer and Identity Store Layer (see figure in the following Microsoft document) and use the existing Identity Manager of ASP.NET Core Identity to work as the authentication and authorization system of the web application.
Custom storage providers for ASP.NET Core Identity
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-3.1To develop the Data Access Layer and Identity Store Layer the following articles might help:
Customize ASP.NET Core Identity
https://raaaimund.github.io/tech/2019/05/13/customize-asp-net-core-identity/Using your own database schema and classes with ASP.NET Core Identity and Entity Framework Core
http://danderson.io/posts/using-your-own-database-schema-and-classes-with-asp-net-core-identity-and-entity-framework-core/Monday, May 31, 2021 12:57 AM -
User-154227990 posted
Hi
Thank you for your response. I am NOT trying to create an authentication and authorization system.
I have just use the example. My original tables are different. because I work in gov so I don't want to disclose anything.
In, short I have many to many tables such as Book, Category, and book category. How can I insert and update using EF core.
I did google but no luck. any example
Monday, May 31, 2021 4:58 AM -
User475983607 posted
learningcode
In, short I have many to many tables such as Book, Category, and book category. How can I insert and update using EF core.Entity Framework is .NET class representation of SQL tables. Rather than writing SQL script you get to interact with data using strong types.
A typical update pattern is fetching the records you wish to update, update the fields you wish to update, finally issue a save. Inserting is much the same except adding entities.
Considering CRUD operations are covered in every beginning level Entity Framework tutorial, it is not clear what problem(s) you are facing. Can you share a code example that illustrates the programming problems you are having?
If you have not written any code, I recommend going through the getting started tutorials on this site. This will give us the same code base and make providing assistance a bit easier. Keep in mind MVC Actions are the same as Web API in .NET Core.
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0
Also keep in mind Web API is just an HTTP interface to a standard .NET method. You have total control over what data is passed to and returned from the HTTP Action.
Monday, May 31, 2021 12:09 PM