C# 保存不公开外键属性/datetime的实体时出错

C# 保存不公开外键属性/datetime的实体时出错,c#,asp.net-mvc-4,entity-framework-4,ef-code-first,C#,Asp.net Mvc 4,Entity Framework 4,Ef Code First,在.NETMVC4中使用EF4时遇到代码优先模型问题。我在VisualStudio2012中,如果有必要的话,连接到远程服务器 每当我尝试使用代码中的一些默认值为数据库设定种子时,我都会遇到以下错误: ---> System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for th

在.NETMVC4中使用EF4时遇到代码优先模型问题。我在VisualStudio2012中,如果有必要的话,连接到远程服务器

每当我尝试使用代码中的一些默认值为数据库设定种子时,我都会遇到以下错误:

---> System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while
saving entities that do not expose foreign key properties for their relationships. The
EntityEntries property will return null because a single entity cannot be identified as
the source of the exception. Handling of exceptions while saving can be made easier by
exposing foreign key properties in your entity types. See the InnerException for
details. 
---> System.Data.UpdateException: An error occurred while updating the entries. See the 
inner exception for details.
---> System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a 
datetime data type resulted in an out-of-range value.
我有一个要求,就是在我的模型中使用guid,但这一行是试图调用以下内容:

//日程表是我已经创建的日程表列表。 scheduleGuids中的foreach变量scheduleGuid { var s=scheduleGuids.IndexOfscheduleGuid; var计划=新计划 { StartDate=新日期时间1989,1,1, EndDate=新日期时间1989,1,7,, Id=调度GUID, 状态=ScheduleStatus.Inactive, 重复性=真 }; var移位=新列表; var-shiftIndex=s*5; var天数=移位指数+5; 对于var si=移位指数;si<天;si++ { Debug.WriteLineschedule{0},shift{1},总移位:{2},s,si>5?si/5:si,si; var shiftGuid=shiftGuids[si]; var d=shiftGuids.IndexOfshiftGuid; var shift=new shift{Schedule=Schedule,Id=shiftGuid}; 开关s { 案例0: shift.StartTime=schedule.StartDate.Addd.Days.At6,30; 轮班时间=8小时+30分钟; 打破 案例1: shift.StartTime=schedule.StartDate.Addd.Days.At6,30; 班次持续时间=10小时+30分钟; 打破 案例2: shift.StartTime=schedule.StartDate.Addd.Days.At8,30; 班次持续时间=10小时+30分钟; 打破 案例3: shift.StartTime=schedule.StartDate.Addd.Days.At9,30; 班次持续时间=10小时+30分钟; 打破 案例4: shift.StartTime=schedule.StartDate.Addd.Days.At11,30; 班次持续时间=10小时+30分钟; 打破 案例5: shift.StartTime=schedule.StartDate.Addd.Days.At22,00; 班次持续时间=10小时+30分钟; 打破 案例6: shift.StartTime=schedule.StartDate.Addd.Days.At20,00; 班次持续时间=10小时+30分钟; 打破 } shift.Description=string.Format{4}:{0}{1}{2},{3}-默认shift,shift.DayOfWeek,shift.StartTime.tostringmmm,shift.DayOfMonth,shift.StartTime.ToStringyyyy,Enum.getnametypeofshift-Type,shift.Type; //context.shift.AddOrUpdateshift; 移位。添加移位; } 时间表。班次=班次; 附表.增补附表; context.Shifts.addor updateshifts.ToArray; context.Schedules.AddOrUpdateschedule; } 它在context.Shifts.AddOrUpdateshifts.ToArray处失败

以下是有问题的模型:

我将所有DateTimes显式设置为datetime2。 我在这些模型的上下文类中使用fluentapi定义了一些其他N:N关系,但所有这些关系都是在Schedule和其他模型之间的。Shift只有一个潜在关系,那就是它与父计划的关系。一个时间表可以有几个班次,所以我的理解是这是一个非常简单的1:N/N:1或0关系


我显然在什么地方漏掉了什么,但我不太清楚在哪里。我知道,直到几个小时前,大部分工作还在进行中,我在建立其他关系时犯了一些错误,但我已经删除了所有这些关系,所有无关的约束和值都从表中删除了。

一旦您将班次添加到计划中,我想您应该只需要调用context.Schedules.AddOrUpdateschedule;
public class Schedule
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("User")]
    public Guid? UserId { get; set; }

    public virtual ScheduleUser User { get; set; }

    public virtual ICollection<ScheduleRule> Rules { get; set; }

    public virtual ICollection<Shift> Shifts { get; set; }

    [Required]
    [DisplayFormat(NullDisplayText = "", DataFormatString= "{0:d}")]
    [Display(Name = "Start Date")]
    public DateTime StartDate { get; set; }

    [Required]
    [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:d}")]
    [Display(Name = "End Date")]
    public DateTime EndDate { get; set; }

    [Required]
    public bool Recurring { get; set; }

    [Required]
    public bool Enabled { get; set; }

    public bool Temporary
    {
        get
        {
            return (ToBeEnabledOn.HasValue && ToBeDisabledOn.HasValue);
        }
    }

    [Column(TypeName = "datetime2")]
    public DateTime? EnabledOn { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? ToBeEnabledOn { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? DisabledOn { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? ToBeDisabledOn { get; set; }

    public ScheduleStatus Status { get; set; } // this is an enum
}

public class Shift
{
    [Key]
    public Guid Id { get; set; }

    public string Description { get; set; }

    public Schedule Schedule { get; set; }

    [ForeignKey("Schedule")]
    public Guid? ScheduleId { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? Start { get; set; }

    [NotMapped]
    public DateTime StartTime
    {
        get
        {
            return (Start ?? DateTime.Now);
        }
        set
        {
            Start = value;
        }
    }

    public long? TimeSpanDurationInTicks { get; set; }

    [NotMapped]
    public TimeSpan Duration
    {
        get
        {
            return new TimeSpan((TimeSpanDurationInTicks ?? 0));
        }
        set
        {
            TimeSpanDurationInTicks = value.Ticks;
        }
    }
}