我有以下EF实体:
public class EmployeeHoliday
{
public int HolidayId { get; set; }
public int EmployeeId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool IsActive { get; set; }
}
public class EmployeeHolidayItem
{
public int HolidayId { get; set; }
public DateTime Date { get; set; }
public int EmployeeId { get; set; }
public string MasterEntity { get; set; }
public bool IsRealHoliday { get; set; }
public bool IsActive { get; set; }
}
EF配置图为:
public EmployeeHolidayMap()
{
// Primary Key
this.HasKey(t => t.HolidayId);
// Properties
this.Property(t => t.HolidayId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(t => t.EmployeeId)
.IsRequired();
// Table & Column Mappings
this.ToTable("employee_holiday");
this.Property(t => t.HolidayId).HasColumnName("holidayid");
this.Property(t => t.EmployeeId).HasColumnName("employeeid");
this.Property(t => t.StartDate).HasColumnName("startdate");
this.Property(t => t.EndDate).HasColumnName("enddate");
this.Property(t => t.IsActive).HasColumnName("isactive");
}
public EmployeeHolidayItemMap()
{
// Primary Key
this.HasKey(t => new { t.HolidayId, t.Date });
this.Property(t => t.EmployeeId)
.IsRequired();
this.Property(t => t.MasterEntity)
.IsRequired();
// Table & Column Mappings
this.ToTable("employee_holiday_item");
this.Property(t => t.HolidayId).HasColumnName("holidayid");
this.Property(t => t.Date).HasColumnName("date");
this.Property(t => t.EmployeeId).HasColumnName("employeeid");
this.Property(t => t.MasterEntity).HasColumnName("masterentity");
this.Property(t => t.IsRealHoliday).HasColumnName("isrealholiday");
this.Property(t => t.IsActive).HasColumnName("isactive");
}
现在,我试图EmployeeHoliday
用几个保存一个新文件,EmployeeHolidayItems
但是我HolidayId
在EmployeeHolidayItems模型中的键上得到了一个错误。
这是用于插入新代码的代码EmployeeHoliday
:
int holidayId = 0;
var newRequest = new Model.EmployeeHoliday();
newRequest.HolidayId = holidayId;
newRequest.EmployeeId = request.EmployeeId;
newRequest.StartDate = dates.FirstOrDefault();
newRequest.EndDate = dates.LastOrDefault();
newRequest.IsActive = true;
var newRequestDates = new List<Model.EmployeeHolidayItem>();
// Add Days to Holiday Item
foreach (var date in dates)
{
var newDate = new Model.EmployeeHolidayItem();
newDate.HolidayId = holidayId;
newDate.EmployeeId = request.EmployeeId;
newDate.MasterEntity = Uow.MasterEntity;
newDate.Date = date;
newDate.IsRealHoliday = true;
newDate.IsActive = true;
// add to list
newRequestDates.Add(newDate);
}
DbContext.EmployeeHoliday.Add(newRequest);
DbContext.EmployeeHolidayItem.AddRange(newRequestDates);
DbContext.SaveChanges();
在插入新记录时,有什么方法可以强制EmployeeHoliday和EmployeeHolidayItem之间的关系?我不想在“映射”中建立关系。
在EmployeeHoliday
类上添加此属性。
public class EmployeeHoliday
{
public ICollection<EmployeeHolidayItem> EmployeeHolidayItems {get;set;}
}
并替换这个
DbContext.EmployeeHoliday.Add(newRequest);
DbContext.EmployeeHolidayItem.AddRange(newRequestDates);
和
newRequest.EmployeeHolidayItems = newRequestDates;
DbContext.EmployeeHoliday.Add(newRequest);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句