我一直试图讨论如何在带有子表的表上添加信息。当我初始化用户时,用户需要加密密码并将数据插入子类。由于我不完全熟悉存储库,工作单元模式,我想确保我第一次正确地做到这一点。谢谢!
我的表是:
User,UserRoles
我的代码:
[HttpPost]
public ActionResult AddUser(User user)
{
try
{
if (ModelState.IsValid)
{
uow.UserRepository.Insert(user);
uow.Save();
return RedirectToAction("Index", "User");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(user);
}
public class UserRepository : IUserRepository, IDisposable
{
private StudentSchedulingEntities _context;
public UserRepository(StudentSchedulingEntities context)
{
if (context == null)
throw new ArgumentNullException("context");
_context = context;
}
public IEnumerable<User> GetUsers()
{
return _context.Users.ToList();
}
public User GetUserByID(int id)
{
return _context.Users.Find(id);
}
public void InsertStudent(User user)
{
_context.Users.Add(user);
}
public void DeleteStudent(int userID)
{
User usr = _context.Users.Find(userID);
_context.Users.Remove(usr);
}
public void UpdateStudent(User user)
{
_context.Entry(user).State = EntityState.Modified;
}
public void Save() {
_context.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_context != null)
{
_context.Dispose();
_context = null;
}
}
}
}
public interface IUserRepository : IDisposable
{
IEnumerable<User> GetUsers();
User GetUserByID(int userID);
void InsertStudent(User user);
void DeleteStudent(int userID);
void UpdateStudent(User user);
void Save();
}
public class UnitOfWork : IDisposable
{
private StudentSchedulingEntities _context = new StudentSchedulingEntities();
private GenericRepository<User> userRepository;
public GenericRepository<User> UserRepository
{
get
{
if (this.userRepository == null)
{
this.userRepository = new GenericRepository<User>(_context);
}
return userRepository;
}
}
public void Save()
{
_context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
从评论更新:
[HttpPost]
public ActionResult AddUser(AddUserViewModel auvm)
{
try
{
if (ModelState.IsValid)
{
uow.UserRoleRepository.Insert(auvm.UserRole);
uow.UserRepository.Insert(auvm.User);
uow.Save();
return RedirectToAction("Index", "User");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(auvm);
}
public class AddUserViewModel
{
public User User { get; set; }
public UserRole UserRole { get; set; }
public UserRoleType UserRoleType { get; set; }
public IEnumerable<SelectListItem> UserRoleTypes { get; set; }
}
答案 0 :(得分:0)
只需创建一个新的User对象即可。将UserRoles分配给用户。保存用户对象。上下文类充当工作单元 - 当您调用Save Changes时,它仅保存 的所有内容。
有些人选择使用ViewModel路线(推荐使用),因此您将发布一个与您的User类非常相似的UserCreateViewModel。这样,如果用户类发生更改,或者您不希望所有内容都暴露给用户,则可以更轻松地处理这些情况。