EF6映射到存储过程的参数太多

时间:2013-05-26 21:16:47

标签: entity-framework

我正在使用EF6 alpha并使用新功能映射到存储过程。在我的单元测试中,我收到错误“过程或函数CreateDayDetail指定了太多参数。”

SQL事件探查器显示以下proc调用:

exec [Schedule].[CreateDayDetail] @startTime=0,@duration=1,@durationEst=0,@isPaid=1,@requirePunch=0,@dayKey=16,@activityTypeKey=1,@TemplateDay_Key=NULL

问题是@TemplateDay_Key参数正在自动生成。但是,我不确定为什么在没有为ActivityType创建一个时创建它。

这是实体类:

public class DayDetail
{
    [Key]
    public int Key { get; set; }

    [Required]
    public TemplateDay Day { get; set; }

    [Required]
    public ActivityType Activity { get; set; }

    [Range(TimeMinimum, TimeMaximum)]
    public int StartTime { get; set; }

    [Range(DurationMinimum, DurationMaximum)]
    public int Duration { get; set; }

    public bool DurationEstimated { get; set; }

    public bool IsPaid { get; set; }

    public bool RequirePunch { get; set; }

    [Timestamp]
    public byte[] Version { get; set; }
}

以下是我用来配置模型的流畅API:

typeConfig.ToTable("Schedule.DayDetail");
typeConfig.Property(d => d.Key)
    .HasColumnName("DayDetailKey");
typeConfig.HasRequired(d => d.Day)
    .WithMany()
    .Map(d => d.MapKey("TemplateDayKey"));
typeConfig.HasRequired(d => d.Activity)
    .WithMany()
    .Map(d => d.MapKey("ActivityTypeKey"));

typeConfig.MapToStoredProcedures(sp =>
    sp.Insert(i => i.HasName("Schedule.CreateDayDetail")
        .Parameter(d => d.Day.Key, "dayKey")
        .Parameter(d => d.Activity.Key, "activityTypeKey")
        .Parameter(d => d.StartTime, "startTime")
        .Parameter(d => d.Duration, "duration")
        .Parameter(d => d.DurationEstimated, "durationEst")
        .Parameter(d => d.IsPaid, "isPaid")
        .Parameter(d => d.RequirePunch, "requirePunch"))
    .Update(u => u.HasName("Schedule.UpdateDayDetail")
        .Parameter(d => d.Key, "key")
        .Parameter(d => d.Day.Key, "dayKey")
        .Parameter(d => d.Activity.Key, "activityTypeKey")
        .Parameter(d => d.StartTime, "startTime")
        .Parameter(d => d.Duration, "duration")
        .Parameter(d => d.DurationEstimated, "durationEst")
        .Parameter(d => d.IsPaid, "isPaid")
        .Parameter(d => d.RequirePunch, "requirePunch")
        .Parameter(d => d.Version, "version"))
    .Delete(x => x.HasName("Schedule.DeleteDayDetail")
        .Parameter(d => d.Key, "key")
        .Parameter(d => d.Day.Key, "dayKey")
        .Parameter(d => d.Activity.Key, "activityTypeKey")
        .Parameter(d => d.Version, "version")));

我今天看了很长时间。我感谢任何帮助.......

2 个答案:

答案 0 :(得分:0)

3个月的问题......

它存在,因为您在模型中使用没有外键的关系。您在模型中创建的关系不在数据库中。

您没有使用:.HasForeignKey(d => d.TemplateDayKey_ID);

您正在使用.Map(d => d.MapKey("TemplateDayKey"));,这需要传递此关系信息以插入和更新存储过程作为参数来处理此关系。

如果在模型中使用基于ForeignKey的关系,您将在存储过程参数列表中看到参数TemplateDayKey_id。

答案 1 :(得分:0)

就我而言,我在模型的数据库中生成了一个属性,EF 试图将此属性作为参数插入,因为我没有正确标记该属性。我需要改变

public string SomeProperty { get; set; }

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string SomeProperty { get; set; }

。 . .然后 EF 停止尝试将该值作为 proc 的一部分插入。