我有以下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
,但我在EmployeeHolidayItems模型中遇到HolidayId
密钥错误。
这是插入新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之间的关系?我不想在我的映射中建立关系。
答案 0 :(得分:0)
在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);