locked
asp.net mvc update multiple tables RRS feed

  • Question

  • User-1097602101 posted

    I have a problem with ASP.NET MVC on accurate update multiple tables. I did add records to different tables like 5 where one is related to the other 4. Please if anyone knows how to become update multiple tables help.

    AllModels.cs(5 tables)

        public partial class City
        {
            public virtual int id_city { get; set; }
            [Display(Name = "City : ")]
            [Required]
            public virtual string city { get; set; }
            [Display(Name = "Address : ")]
            [Required]
            public virtual string address { get; set; }
    
        public virtual  Staff Staff { get; set; }
        }
    
        public partial class Company_info
        {
            public virtual int id_company { get; set; }
            [Display(Name = "Company Name : ")]
            [Required]
            public virtual string name_company { get; set; }
            [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
            [Required]
            [Display(Name = "Start Date: ")]
            public virtual DateTime start_date { get; set; }
            [Required]
            [Display(Name = "EIK/PIK: ")]
            public virtual int eik_pik { get; set; }
            [Required]
            [Display(Name = "City: ")]
            public virtual string city_info { get; set; }
            [Required]
            [Display(Name = "Address: ")]
            public virtual string address_info { get; set; }
            [Required]
            [Display(Name = "Activity: ")]
            public virtual string activity { get; set; }
            [Phone]
            [Required]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a number")]
            [Display(Name = "Phone: ")]
            public virtual string phone_number { get; set; }
            [EmailAddress]
            [Required]
            [Display(Name = "Email: ")]
            public virtual string email { get; set; }
        }
    
        public partial class Company_staff
        {
            public virtual int id_comp_staff { get; set; }
            [Required]
            [Display(Name = "Position: ")]
            public virtual string position { get; set; }
            [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
            [Required]
            [Display(Name = "Start Date: ")]
            public virtual DateTime start_date_com { get; set; }
            [Phone]
            [Required]
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a number")]
            [Display(Name = "Business Phone: ")]
            public virtual string phone_biss { get; set; }
            [Required]
            [Display(Name = "Salary: ")]
            public virtual decimal salary { get; set; }
            [EmailAddress]
            [Required]
            [Display(Name = "Work Email : ")]
            public virtual string email_staff_com { get; set; }
            [Required]
            [Display(Name = "Number day off: ")]
            public virtual int number_day_off { get; set; }
        }
    
        public partial class Qualification
        {
    
            public virtual int id_quali { get; set; }
            [Required]
            [Display(Name = "Skill: ")]
            public virtual string skill { get; set; }
            [Required]
            [Display(Name = "Institute: ")]
            public virtual string institute { get; set; }
            [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
            [Required]
            [Display(Name = "Completion Date: ")]
            public virtual DateTime date_inst { get; set; }
            [Required]
            [Display(Name = "Language: ")]
            public virtual string language { get; set; }
    
    
        }
    
        public partial class Staff
        {
            public virtual int id_staff { get; set; }
            public virtual string first_name { get; set; }
            public virtual string surname { get; set; }
            public virtual string family { get; set; }
            public virtual DateTime date_of_birth { get; set; }
            public virtual string phone_number_staff { get; set; }
            public virtual string email_staff { get; set; }
            public virtual string gender { get; set; }
            public virtual string nationality { get; set; }
        public virtual int fk_city { get; set; }
        public virtual int fk_user { get; set; }
        public virtual int fk_ci { get; set; }
        public virtual int fk_cs { get; set; }
        public virtual int fk_qua { get; set; }
    
        public virtual City City { get; set; }
    
        public virtual Company_info company_info { get; set; }
    
        public virtual Company_staff company_staff { get; set; }
    
        public virtual Qualification qualification { get; set; }
    
        public virtual Users users { get; set; }
    
        }
    
        public partial class Users
        {
            public virtual int id_user { get; set; }
            [Required]
            [Display(Name = "UserName: ")]
            public virtual string user_name { get; set; }
            [Required]
            [Display(Name = "Employee: ")]
            public virtual string employee { get; set; }
            [Required]
            [Display(Name = "UserLevel: ")]
            public virtual string user_level { get; set; }
            [Required]
            [Display(Name = "Password: ")]
            public virtual string password { get; set; }
    
    }

    Function for Create

    public ActionResult Create()
        {
            return View();
        }
    
        // POST: Book/Create
        [HttpPost]
        public ActionResult Create(ViewModel collection)
        {
    
            City c = new City();
            c.city = collection.city;
            c.address = collection.address;
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
                {
                    session.Save(c); //  Save the book in session
                    transaction.Commit();   //  Commit the changes to the database
                }
    
    
            //-------------------------------
    
            int last = c.id_city;
    
            Company_info ci = new Company_info();
    
            //  Creating a new instance of the Book
            ci.name_company = collection.name_company;
            ci.start_date = collection.start_date;
            ci.eik_pik = collection.eik_pik;
            ci.city_info = collection.city_info;
            ci.address_info = collection.address_info;
            ci.activity = collection.activity;
            ci.phone_number = collection.phone_number;
            ci.email = collection.email;
    
    
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
            {
                session.Save(ci); //  Save the book in session
                transaction.Commit();   //  Commit the changes to the database
            }
    
    
    
            //--------------------
            int lastinfo = ci.id_company;
    
            Company_staff cs = new Company_staff();
    
            //  Creating a new instance of the Book
            cs.position = collection.position;
            cs.start_date_com = collection.start_date_com;
            cs.phone_biss= collection.phone_biss;
            cs.salary = collection.salary;
            cs.email_staff_com = collection.email_staff_com;
            cs.number_day_off = collection.number_day_off;
    
    
    
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
            {
                session.Save(cs); //  Save the book in session
                transaction.Commit();   //  Commit the changes to the database
            }
            //--------------------
    
            int laststaff = cs.id_comp_staff;
    
            Qualification q = new Qualification();
    
            //  Creating a new instance of the Book
            q.skill = collection.skill;
            q.institute = collection.institute;
            q.date_inst = collection.date_inst;
            q.language = collection.language;
    
    
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
            {
                session.Save(q); //  Save the book in session
                transaction.Commit();   //  Commit the changes to the database
            }
    
            int lastqua = q.id_quali;
    
            Users u = new Users();
    
            //  Creating a new instance of the Book
            u.user_name = collection.user_name;
            u.employee = collection.employee;
            u.user_level = collection.user_level;
            u.password = collection.password;
    
    
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
            {
                session.Save(u); //  Save the book in session
                transaction.Commit();   //  Commit the changes to the database
            }
    
            int lastuser = u.id_user;
    
            Staff s = new Staff();
    
            //  Creating a new instance of the Book
            s.first_name = collection.first_name;
            s.surname = collection.surname;
            s.family = collection.surname;
            s.date_of_birth = collection.date_of_birth;
            s.phone_number_staff = collection.phone_number_staff;
            s.email_staff = collection.email_staff;
            s.gender = collection.gender;
            s.nationality = collection.nationality;
            s.fk_city = last;
            s.fk_ci = lastinfo;
            s.fk_qua = lastqua;
            s.fk_user = lastuser;
            s.fk_cs = laststaff;
    
    
            // TODO: Add insert logic here
    
            using (ITransaction transaction = session.BeginTransaction())   //  Begin a transaction
            {
                session.Save(s); //  Save the book in session
                transaction.Commit();   //  Commit the changes to the database
            }
            return RedirectToAction("Index");
    
        }

    Funtion for Edit but did not work

    public ActionResult Edit(int id,int idd)
        {
    
            Staff c = new Staff();
            c = session.Query<Staff>().Where(b => b.id_staff == id).FirstOrDefault();
    
            City s = new City();
            s = session.Query<City>().Where(b => b.id_city == idd).FirstOrDefault();
    
            ViewBag.SubmitAction = "Save";
            return View(s);
        }
    
        // POST: Book/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, ViewModel collection)
        {
    
                City c = new City();
                c.id_city = id;
                c.city = collection.city;
                c.address = collection.address;
    
    
    
                // TODO: Add insert logic here
    
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(c);
                    transaction.Commit();
                }
    
             /*  
    
            Company_info ci = new Company_info();
    
            //  Creating a new instance of the Book
            ci.id_company = collection.id_company;
            ci.name_company = collection.name_company;
            ci.start_date = collection.start_date;
            ci.eik_pik = collection.eik_pik;
            ci.city_info = collection.city_info;
            ci.address_info = collection.address_info;
            ci.activity = collection.activity;
            ci.phone_number = collection.phone_number;
            ci.email = collection.email;
    
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(ci);
                transaction.Commit();
            }
    
            Company_staff cs = new Company_staff();
    
            //  Creating a new instance of the Book
            cs.id_comp_staff = collection.id_comp_staff;
            cs.position = collection.position;
            cs.start_date_com = collection.start_date_com;
            cs.phone_biss = collection.phone_biss;
            cs.salary = collection.salary;
            cs.email_staff_com = collection.email_staff_com;
            cs.number_day_off = collection.number_day_off;
    
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(cs);
                transaction.Commit();
            }
    
    
            Qualification q = new Qualification();
    
            //  Creating a new instance of the Book
            q.id_quali = collection.id_quali;
            q.skill = collection.skill;
            q.institute = collection.institute;
            q.date_inst = collection.date_inst;
            q.language = collection.language;
    
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(q);
                transaction.Commit();
            }
    
            Users u = new Users();
    
            //  Creating a new instance of the Book
            u.id_user = collection.id_user;
            u.user_name = collection.user_name;
            u.employee = collection.employee;
            u.user_level = collection.user_level;
            u.password = collection.password;
    
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(u);
                transaction.Commit();
            }
            */
            Staff s = new Staff();
    
            //  Creating a new instance of the Book
            s.id_staff = collection.id_staff;
            s.first_name = collection.first_name;
            s.surname = collection.surname;
            s.family = collection.surname;
            s.date_of_birth = collection.date_of_birth;
            s.phone_number_staff = collection.phone_number_staff;
            s.email_staff = collection.email_staff;
            s.gender = collection.gender;
            s.nationality = collection.nationality;
    
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(s);
                transaction.Commit();
            }
            return RedirectToAction("Index");
    
        }
    Tuesday, August 13, 2019 4:26 PM

All replies

  • User1520731567 posted

    Hi sssaa,

                using (ITransaction transaction = session.BeginTransaction())
    

    What is your session?

    And what is error message?

    I find your I found that your models lack primary key.

    You could try to add [Key] attribute on your primary key firstly,like:

    [Key]        
    public virtual int id_staff { get; set; }

    If you still have any questions,please post more details.

    Best Regards.

    Yuki Tao

    Wednesday, August 14, 2019 7:15 AM
  • User-1097602101 posted

    When I remove the virtual it gives me an error. I'm fine with putting the key on the primary key, but how do I do when I hit edit to check fk_city if it matches the id_city of the City table to let me change it. I do it like that, but I can't do it to return both s and c at the same time.

    public ActionResult Edit(int id)
            {
                City c = new City();
                c = session.Query<City>().Where(b => b.id_city == id).FirstOrDefault();
                Staff s = new Staff();
                s = session.Query<Staff>().Where(b => b.id_staff == id).FirstOrDefault();
    
    
      
      return View(new All() {City=c,Staff=s});

    Class All 

    public partial class All
            {
    
            public virtual City City { get; set; }
    
            public virtual Company_info company_info { get; set; }
    
            public virtual Company_staff company_staff { get; set; }
    
            public virtual Qualification qualification { get; set; }
    
            public virtual Users users { get; set; }
            public virtual Staff Staff { get; set; }
    
        }



    Wednesday, August 14, 2019 10:26 AM
  • User1520731567 posted

    Hi sssaa,

    When I remove the virtual it gives me an error. I'm fine with putting the key on the primary key, but how do I do when I hit edit to check fk_city if it matches the id_city of the City table to let me change it.

    I suggest you could refer to this tutorial about Configure One-to-Many Relationships in EF 6.

    You could know how to configure relationships between multiple entities.

    You don't have to explicitly add foreign key properties to your model, just add [ForeignKey("XXX")] to the relevant primary key.

    For example:

    public class Student
    {
    [Key] public int StudentId { get; set; } public string StudentName { get; set; } public virtual StudentAddress Address { get; set; } } public class StudentAddress { [ForeignKey("Student")] public int StudentAddressId { get; set; } //apply [ForeignKey("Student")] on the StudentAddressIdproperty which will make it a foreign key for the Student entity public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public int Zipcode { get; set; } public string State { get; set; } public string Country { get; set; } public virtual Student Student { get; set; } }

    Student includes the StudentAddress navigation property

    and StudentAddress includes the Student navigation property.

    With the one-to-zero-or-one relationship.

    Best Regards.

    Yuki Tao

    Thursday, August 15, 2019 1:55 AM