My platform has two tables UserInfo
and PersonalInfo
. When a user creates a profile the data is saved in UserInfo
. The view for UserInfo
has an @Html.EditorFor
, which is auto populated from Active Directory(if the AD field is null, a helper class adds N/A in its place). Depending on what application the user decides to use within the platform, more information will be needed so they will need to create a row in PersonalInfo
. The view for PersonalInfo
as a @Html.DropDownListFor
that the user can choose from which will update that users department and cost center.
I would like to update the department for UserInfo
when the row for PersonalInfo
is created or updated.
Currently when I create a row it looks like this:
if (ModelState.IsValid)
{
var myUserInfo = new myUserInfo
{
UserId = userNum.Id,
Address = viewModel.Address,
City = viewModel.City,
State = viewModel.State,
CellPhone = viewModel.CellPhone,
Department = viewmodel.Department,
CostCenter = viewmodel.CostCenter,
ZipCode = viewModel.ZipCode
};
db.MyUserInfos.Add(myUserInfo);
db.SaveChanges();
}
How do I update the Department
field only in the PersonalInfo
table before db.SaveChanges()
is called?
add a row in tableB:-
var tableB = new TableB
{
UserId = userNum.Id,
Address = viewModel.Address,
City = viewModel.City,
State = viewModel.State,
CellPhone = viewModel.CellPhone,
Department = viewmodel.Department,
CostCenter = viewmodel.CostCenter,
ZipCode = viewModel.ZipCode
};
db.MyTableB.Add(tableB);
update a row in tableA:-
var tableA = db.MyTableA.Find(tableAId);
tableA.Department = viewModel.Department;
db.MyTableA.Attach(tableA);
db.Entry(tableA).State = EntityState.Modified
once you have made all the your required changes, call db.SaveChanges();