I am very new to MVC so I have some confusions regarding models when we have relations between multple tables.Formally I was using views or stored procedures of sql to manipulate data but now I am using Entity Framework so confused how my model should look like ?
I have a table for Users , UserImages , UserRoles . UserImages and UserRoles is referring to UserID from Users table.I am not sure how my model should be ? While displaying data I generally use a view created in sql server by joining these 2 tables. And when an user is edited or created I update these 3 tables.
Not sure what should my model look like in this case ? Do I create a sql server view and add it to edmx ? Or add 3 tables seperately in EDMX and create custom properties in User model for those other 2 tablels?
[HttpPost] public ActionResult Create(UserFormViewModel UserView) {
User user = UserView.User;
try {
if (ModelState.IsValid) {
repository.AddUser(user);
repository.Save();
return View(new UserFormViewModel(user));
} else {
return View(new UserFormViewModel(user));
}
} catch (Exception ex) {
ModelState.AddModelError("Error", ex.Message);
return View(new UserFormViewModel(user));
}
}
public class UserFormViewModel {
UucsrRepository repository = new UucsrRepository();
public User User { get; private set; }
public SelectList States { get; private set; }
public SelectList Genders { get; private set; }
public SelectList RolesLists { get; private set; }
public SelectList SelectedRolesLists{ get; private set; }
public UserFormViewModel(User contact) {
User = contact;
States = new SelectList(repository.GetAllStates() ,"ShortName","Name");
Genders = new SelectList(repository.GetAllGenders(), "Gender", "Description");
RolesLists = new SelectList(repository.GetAllRolesLists(), "Id", "Name");
}
}
I am not sure how should I exactly handle the Adding role list and images here .