EF 6禁用保存相关实体(虚拟)

本文关键字:实体 虚拟 保存 EF | 更新日期: 2023-09-27 18:15:19

我在使用实体框架6的应用程序中有一个问题

我的一个EF实体是:
public class EmployeeHoliday
    {
        public EmployeeHoliday()
        {
            this.Employee = new Model.Employee();
        }
        public int HolidayId { get; set; }
        public int EmployeeId { get; set; }
        public byte HolidayTypeId { get; set; }
        public string MasterEntity { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime RequestDate { get; set; }
        public int CreatedBy { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool NonContinuous { get; set; }
        public bool RequestInAdvance { get; set; }
        public bool Authorized { get; set; }
        public Nullable<int> AuthorizedBy { get; set; }
        public DateTime? AuthorizedDate { get; set; }
        public string AuthorizedNotes { get; set; }
        public Nullable<int> DaysPending { get; set; }
        public Nullable<int> DaysUsed { get; set; }
        public Nullable<int> DaysApproved { get; set; }
        public bool IsActive { get; set; }
        public virtual Employee Employee { get; set; }
    }

这个实体与Employee EF实体有关系,但我并没有在所有情况下加载它。

现在我正试图插入一个新的EmployeeHoliday,我得到一个错误,看看实体框架日志(在控制台中),我可以看到正在尝试更新Employee

我是这样做的:

// insert
                var newRequest = new Model.EmployeeHoliday();
                newRequest.EmployeeId = request.EmployeeId;
                newRequest.MasterEntity = Uow.MasterEntity;
                newRequest.CreatedBy = Uow.UserId;
                newRequest.CreatedDate = DateTime.Now;
                newRequest.Authorized = false;
                newRequest.HolidayTypeId = request.HolidayTypeId;
                newRequest.RequestDate = request.RequestDate;
                newRequest.NonContinuous = !continuousDays;
                newRequest.StartDate = dates.FirstOrDefault();
                newRequest.EndDate = dates.LastOrDefault();
                newRequest.RequestInAdvance = request.RequestInAdvance;
                newRequest.IsActive = true;
Uow.HolidayRepository.Add(newRequest);
DbContext.SaveChanges();

控制台日志:

INSERT [dbo].[employee]([employerid], [memberid], [masterentity], [joindate], [employeenumber], [employeeformat], [leavedate], [leavetype], [credentialnumber], [employeetypeid], [employeegroupid], [jobtitleid], [profitcenterid], [locationid], [shiftcode], [timepolicyid], [holidaypolicyid], [overtimepolicyid], [supervisorid], [holidaytemplateid], [patterncode], [ispunishable], [isactive])
VALUES (@0, @1, NULL, @2, @3, NULL, NULL, NULL, NULL, NULL, @4, @5, @6, @7, NULL, @8, NULL, NULL, NULL, NULL, NULL, @9, @10)
SELECT [employeeid]
FROM [dbo].[employee]
WHERE @@ROWCOUNT > 0 AND [employeeid] = scope_identity()

我要插入的不是Employee,而是EmployeeHoliday

那么错误当然是:

Cannot insert the value NULL into column 'masterentity', table 'dbo.employee'; column does not allow nulls. INSERT fails.

我能告诉EF不要保存我的相关实体吗?

EF 6禁用保存相关实体(虚拟)

当创建新的EmployeeHoliday时,您还可以在构造函数中创建Employee实例。

试着把它设为null,看看是否有效。

newRequest.EmployeeId = request.EmployeeId;
newRequest.Employee = null;