0
votes

I have a two tables

RegisterTable : FirstName
                LastName
                UserName
                Password

UserLoginTable : UserName
                 Password

I want to insert the datas into multiple tables from same control. I am inserting data into Register Table and simaltaneously I want the data to be inserted into UserLogin Table but only the UserName and Password into UserLogin Table as my UserLogin Table contains ony UserName and Password Column.

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();

        }

My Both Models

 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
What database are you using? MSSQL? - Ashley Medway
@AshleyMedway Yes MSSQL. - Sam
Hi @AshleyMedway many thanks but i want this function to operate in asp.net mvc(C#) not using sql query. - Sam
Sam, do you mean using entity framework? It would be helpful if you could provide examples of what it is you do now to insert the data. - Ashley Medway
@AshleyMedway Actually I am using entity framework for this one. So, yes. - Sam

2 Answers

0
votes

I'm assuming that you have used Entity Framework and have the Database Context property somewhere, hence you can insert data into multiple tables using this way. Simplifying your code to make it look more readable.

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
    }
0
votes

If i understand it correctly.

You can insert the data in UserLoginTable in same function where you are inserting data in RegisterTable.

One way would be creating anonymous UserTableDTO object from RegisterDTO object and adding it using Add function, something like this

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