0

我正在使用带有 MySQL db 的 ASP Core Web 应用程序、MVC、Code First。这是我的数据库的结构: 在此处输入图像描述

这是这两个模型/类的代码:

=====此代码适用于第二个 EmployeeDate 表(儿童)=====

[Table("employeedate")]
    {
        [Key]
        [Required]
        [Column("ID", TypeName = "int(10)")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Required]
        [Column("HiredDate", TypeName = "date")]
        public DateTime HiredDate { get; set; }

        [Column("FiredDate", TypeName = "date")]
        public DateTime? FiredDate { get; set; }

        [Required]
        //[Timestamp]
        [Column("ModifiedDate", TypeName = "datetime")]
        public DateTime ModifiedDate { get; set; }

        [Required]
        [Column("EmployeeID", TypeName = "int(10)")]
        public int EmployeeID { get; set; }

        // This attribute specifies which database field is the foreign key.
        [ForeignKey(nameof(EmployeeID))]

        // InverseProperty links the two virtual properties together.
        [InverseProperty(nameof(Models.Employee.EmployeeDates))]
        public virtual Employee Employee { get; set; }

    }
    
=====This code is for 1st Employee Table (Parent) =====

 [Table("employee")]
    public class Employee
    { 
        public Employee()
        {
            EmployeeDates = new HashSet<EmployeeDate>();
        }

        public string GetHiredDate
        {
            get
            {
                var hiredate = "this employee has never been hired";
                if (EmployeeDates.LastOrDefault() != null)
                {
                    hiredate = EmployeeDates.LastOrDefault().HiredDate.ToLongDateString();

                }
                return hiredate;
            }
        }
        
        public string GetFiredDate
        {
            get
            {
                var firedate = "active";
                if (EmployeeDates.LastOrDefault() != null && EmployeeDates.LastOrDefault().FiredDate.HasValue)
                {
                    firedate = EmployeeDates.LastOrDefault().FiredDate.Value.ToLongDateString();
                }
                return firedate;
            }
        }
        private DateTime hireddate = DateTime.Now;
        public DateTime HiredDate
        {
            get
            {
                return hireddate;
            }
        }
        public DateTime? FiredDate
        {
            get
            {
                DateTime? fireddate = null;
                if (EmployeeDates.LastOrDefault() != null && EmployeeDates.LastOrDefault().FiredDate.HasValue)
                {
                    fireddate = EmployeeDates.LastOrDefault().FiredDate.Value;
                }
                return fireddate;
            }
        }

        [Key]
        [Required]
        [Column("ID", TypeName = "int(10)")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Required]
        [Column("FirstName", TypeName = "varchar(60)")]
        public string FirstName { get; set; }

        [Required]
        [Column("LastName", TypeName = "varchar(60)")]
        public string LastName { get; set; }

        [Required]
        [Column("Email", TypeName = "varchar(100)")]
        public string Email { get; set; }

        [Required]
        [Column("Phone", TypeName = "varchar(20)")]
        public string Phone { get; set; }

        [Required]
        [Range (16, 100)]
        [Column("Age", TypeName = "int(1)")]
        public int Age { get; set; }

        [Required]
        [Column("City", TypeName = "varchar(100)")]
        public string City { get; set; }

        [Required]
        [Column("Department", TypeName = "varchar(100)")]
        public string Department { get; set; }

        [InverseProperty(nameof(Models.EmployeeDate.Employee))]
 
        public virtual ICollection<EmployeeDate> EmployeeDates { get; set; }   
    }

CREATE 方法工作正常,包括 HiredDate (Not FiredDate)。但 EDIT 仅适用于 Employee 属性, FiredDate 属性不起作用。我可以更新其他所有内容,例如名字、年龄等,但不能更新 FiredDate。

这是控制器内部的代码:

// GET: Employee/Create
        public IActionResult Create(int ID =0)
        {
            return View(new Employee());
        }
        // POST: Employee/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(string firstName, string lastName, string email, string phone, int age, string city, string department, DateTime HiredDate)
        {
            //Nov20 add 2 objects and append HireDate separately from another Table
            var employee = new Employee { FirstName = firstName, LastName = lastName, Email = email, Phone = phone, Age = age, City = city, Department = department };
            var employeeDate = new EmployeeDate { HiredDate = HiredDate };
            employee.EmployeeDates.Add(employeeDate);
            if (ModelState.IsValid)
            {
                _context.Add(employee);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(employee);
        }
        // GET: Employee/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            var employee = await _context.Employees.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }
            return View(employee);
        }
        // POST: Employee/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("ID,FirstName,LastName,Email,Phone,Age,City,Department,FiredDate")] Employee employee)
        {
            if (id != employee.ID)
            {
                return NotFound();
            }
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(employee.ID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(employee);
        }

最后查看:====编辑==== 在此处输入图像描述

====创建==== 在此处输入图像描述

顺便说一句,当在 Create View 中注释掉 FiredDate div 时(因为那里不需要它),我的项目无法立即构建。

关于我做错了什么的任何建议?我是这种 ASP Code First 方法的新手。这里还有一些照片: 在此处输入图像描述

4

0 回答 0