使用实体框架从单个控件将数据输入多个表的过程

时间:2018-08-20 07:17:40

标签: c# sql-server asp.net-mvc

我有两个桌子

RegisterTable : FirstName
                LastName
                UserName
                Password

UserLoginTable : UserName
                 Password

我想从同一控件将数据插入到多个表中。我正在将数据插入到寄存器表中,同时希望将数据插入到UserLogin表中,但仅将UserName和Password插入UserLogin表中,因为我的UserLogin表只包含UserName和Password列。

My Conntroller
 public class RegisterController : Controller
{



        private IRegisterService registerService;


        public RegisterController(IRegisterService _registerService)
        {
        registerService = _registerService;

        }
        //GET: Register
      public ActionResult Index()
    {
        List<RegisterDTO> registers = registerService.GetAllRegisters();
        return View(registers.ToList());
    }

   [HttpGet]
   public ActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Create(RegisterDTO registerDTO)
    {
        try
        {
            bool isSaved;
            if (registerDTO.Id == 0)
                isSaved = registerService.Create(registerDTO);

            else

                return View();

        }

我的两个模特

 public class RegisterDTO
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }


}
 public class UserLoginDTO
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确的话。

您可以将数据插入UserLoginTable中,其功能与在RegisterTable中插入数据的功能相同。

一种方法是从RegisterDTO对象创建匿名UserTableDTO对象,然后使用Add函数将其添加,类似这样

Context.UserLoginDTOs.Add(new UserLoginDTO()
                {
                    UserName = registerDTO.UserName,
                    Password = registerDTO.Password
                });

答案 1 :(得分:0)

我假设您使用过Entity Framework并在某处具有Database Context属性,因此您可以使用这种方式将数据插入到多个表中。简化代码以使其更具可读性。

public class RegisterController : Controller
    {
        #region Properties
        private IRegisterService _registerService;
        private readonly RegisterDbContext _registerDbContext;   //Replace it with your custom <DbContext> class
        #endregion

        #region Constructor
        public RegisterController(IRegisterService registerService)
        {
            _registerService = registerService;
            _registerDbContext = new RegisterDbContext();
        }
        #endregion

        #region Actions
        [HttpPost]
        public ActionResult Create(RegisterDTO registerDTO)
        {
            //You can use Db Transactions to add data 
            using (var dbContextTransaction = _registerDbContext.Database.BeginTransaction())
            {
                try
                {
                    //Assuming this class as Database Entity refering to Register table in Database
                    Register register = new Register();
                    register.FirstName = registerDTO.FirstName;
                    register.LastName = registerDTO.LastName;
                    register.UserName = registerDTO.UserName;
                    register.Password = registerDTO.Password;
                    _registerDbContext.Register.Add(register);
                    _registerDbContext.SaveChanges();

                    //Assuming this class as Database Entity refering to UserLogin table in Database
                    UserLogin userLogin = new UserLogin();
                    userLogin.UserName = registerDTO.UserName;
                    userLogin.Password = registerDTO.Password;
                    _registerDbContext.UserLogin.Add(userLogin);
                    _registerDbContext.SaveChanges();


                    dbContextTransaction.Commit();
                    return View();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                }
            }
        }
        #endregion
    }