模型类:
public class BulkAttendance
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int EmployeeCategoryId { get; set; }
public int EmployeeLabourGroup { get; set; }
public int CompanyId { get; set; }
public int BranchId { get; set; }
public int FinancialYearId { get; set;}
public int ApprovalStatus { get; set; }
public int ApprovalLevel { get; set; }
public DateTime? ApprovedDate { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public int UserId { get; set; }
public int CurrencyPrecision { get; set; }
public List<BulkAttendaceDetail> AttendanceDetail { get; set; }
}
public class BulkAttendaceDetail
{
public string AttendanceId { get; set; }
public string FullName { get; set; }
public DateTime DateWorked { get; set; }
public DateTime LoginTime { get; set; }
public DateTime LogoutTime { get; set; }
public int Employeeid { get; set; }
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public int Category { get; set; }
public int WorkId { get; set; }
public decimal Amount { get; set; }
public int Work { get; set; }
public decimal OverTime { get; set; }
public decimal OverTimeAmount { get; set; }
public string WorkCategoryName { get; set; }
public string WorkShortName { get; set; }
public decimal OvertimeRate { get; set; }
public DateTime TimeIn { get; set; }
public DateTime TimeOut { get; set; }
public decimal SalaryAmount { get; set; }
}
我正在尝试在我的项目中发布
employeeAttendance
。问题是 BulkAttendance
模型 ID 是数据库生成的。我想将 Id
值分配给 AttendanceId
详细模型中的 BulkAttendance
- 我该如何处理这种情况?
这就是我尝试将员工的
Attendance
插入数据库的方式
public async Task BulkAttendanceEntry(List<BulkAttendance> attendances)
{
try
{
foreach (var attendance in attendances)
{
await _bulkAttendanceContext.tbl_AttendanceMaster.AddAsync(attendance);
if (attendance.AttendanceDetail != null && attendance.AttendanceDetail.Any())
{
foreach(var attendanceDetail in attendance.AttendanceDetail)
{
}
}
}
}
catch(Exception)
{
throw;
}
}
在 Entity Framework Core 中,如果您想在像您这样的两个实体之间定义“一对多”关系,则必须在子实体 BulkAttendanceDetail 中添加对父实体 BulkAttendance 的引用。因此,将在子表中创建一个外键,指向父表。当您将新实体正确添加到 BulkAttendanceDetail 表时,新行将具有一个引用属性(外键),其中包含父 BulkAttendance 表中其父行的相应 ID。
要实现此机制,您必须修改 BulkAttendanceDetail 类:
public class BulkAttendanceDetail
{
[Key]
public int Id { get; set; } //primary key of BulkAttendanceDetail table
public int BulkAttendanceId { get; set; } //foreign key to BulkAttendance table
BulkAttendance BulkAttendance { get; set; } //parent class
public string FullName { get; set; }
public DateTime DateWorked { get; set; }
public DateTime LoginTime { get; set; }
public DateTime LogoutTime { get; set; }
public int Employeeid { get; set; }
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public int Category { get; set; }
public int WorkId { get; set; }
public decimal Amount { get; set; }
public int Work { get; set; }
public decimal OverTime { get; set; }
public decimal OverTimeAmount { get; set; }
public string WorkCategoryName { get; set; }
public string WorkShortName { get; set; }
public decimal OvertimeRate { get; set; }
public DateTime TimeIn { get; set; }
public DateTime TimeOut { get; set; }
public decimal SalaryAmount { get; set; }
}
属性将是您的外键。当您向上下文添加一个包含已填充子列表的新 BulkAttendance 对象(父对象)时:
List<BulkAttendanceDetail> AttendanceDetail
外键将在保存新父对象时自动生成:
await _bulkAttendanceContext.SaveChangesAsync();