I have 4 tables. OperationTable, ClientTable, ClientDetails, OperationRes
ClientTable
- ClientID
- Name
- Surname
- Birthday
- VerNumber
ClientDetails
- ClientID
- Adress
- Telephone
OperationTable
- OperationID
- Date
- Time
- ClientID
OperationRes
- ResID
- OperationID
- Name
- Type
- No
i have page where we ask Client to fill a form to register for smth. Everything must be in one page and after Client submit the form, we must insert all data to it's table. Date and Time to OperationTable, Name and Surname to ClientTable and so on. I'm new in ASP.NET MVC. i have tried to use "Code Fisrt". I have created Model and just used it to autogenerate View and Controller. but it's not what i want. i've found this Tutorial. it works! But i have more than 4 tables which and more rows than i write above. what is the best solution?
Logically, your operation will be exactly same as the tutorial show. only you have to create ViewModel which contains all the 4 table fields.
Then when the form post back, do your logic of deciding which field in ViewModel goes to which tables' model. Then save that table model.
In the tutorial it use one ViewModel (LoginViewModel) and save to two table (Login, User). In you case just save to 4 (OperationTable, ClientTable, ClientDetails, OperationRes).
You want a view model that holds all the data you want to insert, then in your controller create the objects based on that view model and insert using EF. Something like:
Then in your controller, use your ViewModel to populate your entities, then insert using EF:
You may want to look at tidying up the entity framework code using a Repository Pattern and you could also look at automapper to map entities from your viewmodel, to save doing it manually.