....................Please be patient its a story..........
I am using code first approach in my mvc project. i want to know what is more practical approch for my condition
i have a candidate table which is connected with four or five more tables Via Can Id (One to many Relation)
Candidate class(code first class) has fields related to connected tables e.g
public int SelectedEdu {get;set;}
public IEnumerable
more classes are there i am showing only one.
using entityframwork i am fetching education related to that candidate via Can Id (this shows no join and no viewModel usage)
Edu is obviously a list which i am showing on a View related to that candidtae,. by any way (i am not going to show HOW hope that clears,as my code working well).
On the other hand if i wish to use ViewModels i have to join education table with candidate table w.r.t. Can ID(again i am not showing code.assume a linq query to join as i am in the developers world, every single reader is aware of that)
anyway by getting record in a viewmodel i am just facing an issue that for the first time if candidate go to his/her profile there would be no matching candidate Id in Education table as it is the first time user login OR by any other reason eduction table is empty regarding that candidate so NO join is possible here also.
what i did to handle this thing is at the time of sign up i generated CanId in education table after record enter to candidate table.By doing this education table got CanId With all other null values(Here Linq join query comes into picture and works)
and everything is working fine in both the conditions
i want to know which approach is more practical to use ViewModels or Code First Model Classes in industry
umair mohsinPosted Apr 29, 2024, 12:31 PM
one question is that is it OK to generate ID in a related table at sign up in oreder to get id and establishing a joining relation between tables.
Naimish MakwanaPosted Apr 29, 2024, 4:29 AM
The choice between using ViewModels and Code First Model Classes often depends on the specific requirements of your project and the complexity of the data you are dealing with. Here are some considerations:
ViewModels: ViewModels can be very useful when you need to pass data from multiple entities to a view. They allow you to shape multiple entities into a single object, which can be very helpful for complex views. However, as you’ve noticed, they can become complicated when dealing with relationships and ensuring data integrity.
Code First Model Classes: The Code First approach is great for defining your database structure through your code, which can lead to cleaner and more manageable code. It’s particularly useful when your database schema is likely to change often, as these changes can be managed through code migrations. However, it might not be as flexible when dealing with complex views that require data from multiple entities.
In the industry, both approaches are widely used. It’s common to use a combination of both, depending on the situation. For example, you might use Code First to define your database schema and relationships, and then use ViewModels to shape the data for complex views.
In your case, since you’re dealing with related data from multiple tables, using a ViewModel could be beneficial. However, if you find that managing the relationships and ensuring data integrity is becoming too complex with ViewModels, it might be worth considering using Code First Model Classes.
Thanks