How to properly handle insertion in two tables with dependent data (Entity+API+MVCapp) ?
Hello, I'm new to both programming and .NET development, so pardon my ignorance. But I'm studying this project where there is a WebAPI in conjunction with a MVC web application. ORM I'm using is Entity with SQL Server.
Simply, let's say we have a Book (BookID [pk], Description) and a BookHistory (BookHistoryID [pk], BookID [fk], Situation, Date) entity/model.
The question is what is the correct implementation for the case when you create a new book record, the application should start the BookHistory records by adding the 'created' record on it.
I only think sequentially, lol, so my mind says I should async-ly call the book creation API url in the controller of the MVC application and if all went fine (if not, present error) I grab the bookID of the recently created book (returned by the API) and insert a new record to BookHistory. But, if the BookHistory record insertion doesn't work somehow, I delete the Book record and present an error do the user. Any other way of doing this conflicts with my idea that I need the BookID first to update the other relational data (in this case, the BookHistory)
I feel since the tables are tied in Entity, there is probably a better way on doing this on the API repository and not in application using two API calls... or maybe using a trigger in SQL Server but I couldn't find (or couldn't search Google) any documentation that might help me in this case.
Any help is deeply appreciated. Thanks in advance.
0 comments:
Post a Comment