EF6 Code First FOREIGN KEY错误

时间:2014-05-29 14:29:37

标签: asp.net-mvc entity-framework properties navigation

我的代码很简单我有3个类:

[DataContract]
public class PhaseDocument : BaseModelEntity
{
    /// <summary>
    /// Gets or sets the phase document identifier.
    /// </summary>
    /// <value>
    /// The phase document identifier.
    /// </value>
    [Key]
    [Required]
    [DataMember]
    public Guid PhaseDocumentId { get; set; }

    /// <summary>
    /// Gets or sets the dynamic phase header identifier.
    /// </summary>
    /// <value>
    /// The dynamic phase header identifier.
    /// </value>
    [Required]
    [DataMember]
    public Guid DynamicPhaseHeaderId { get; set; }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>
    /// The version.
    /// </value>
    [Required]
    [DataMember]
    public  int Version   { get; set; }

    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    /// <value>
    /// The description.
    /// </value>
    [Required]
    [DataMember]
    public string Description { get; set; }


    /// <summary>
    /// Gets or sets the HTML code.
    /// </summary>
    /// <value>
    /// The HTML code.
    /// </value>
    [Required]
    [DataMember]
    public string HtmlCode { get; set; }

    /// <summary>
    /// Gets or sets the attachments identifier.
    /// </summary>
    /// <value>
    /// The attachments identifier.
    /// </value>
    [Required]
    [DataMember]
    public int AttachmentsId { get; set; }

    /// <summary>
    /// Gets or sets the child of.
    /// </summary>
    /// <value>
    /// The child of.
    /// </value>
    [DataMember]
    public Guid? ChildOf { get; set; }
    /// <summary>
    /// Gets or sets the work flow instance identifier.
    /// </summary>
    /// <value>
    /// The work flow instance identifier.
    /// </value>
    [DataMember]
    public Guid WorkFlowInstanceId { get; set; } 
    /// <summary>
    /// Gets or sets the phase modules.
    /// </summary>
    /// <value>
    /// The phase modules.
    /// </value>
    public virtual ICollection<PhaseModule> PhaseModules { get; set; }

    /// <summary>
    /// Gets or sets the dynamic phase header.
    /// </summary>
    /// <value>
    /// The dynamic phase header.
    /// </value>
    public virtual DynamicPhaseHeader DynamicPhaseHeader { get; set; }


    /// <summary>
    /// Gets or sets the attachments.
    /// </summary>
    /// <value>
    /// The attachments.
    /// </value>
    public virtual Attachments Attachments { get; set; }
    /// <summary>
    /// Gets or sets the work flow instance.
    /// </summary>
    /// <value>
    /// The work flow instance.
    /// </value>
    public virtual WorkFlowInstance WorkFlowInstance { get; set; }


}
public class PhaseModule :BaseModelEntity
{
    /// <summary>
    /// Gets or sets the phase module identifier.
    /// </summary>
    /// <value>
    /// The phase module identifier.
    /// </value>
    [Key]
    [Required]
    [DataMember]
    public Guid PhaseModuleId { get; set; }

    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    /// <value>
    /// The description.
    /// </value>
    [Required]
    [DataMember]
    public string Description { get; set; }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>
    /// The version.
    /// </value>
    [Required]
    [DataMember]
    public int Version { get; set; }

    /// <summary>
    /// Gets or sets the phase document identifier.
    /// </summary>
    /// <value>
    /// The phase document identifier.
    /// </value>
    [Required]
    [DataMember]
    public Guid PhaseDocumentId { get; set; }


    /// <summary>
    /// Gets or sets the HTML code.
    /// </summary>
    /// <value>
    /// The HTML code.
    /// </value>
    [Required]
    [DataMember]
    public string HtmlCode { get; set; }


    /// <summary>
    /// Gets or sets the order position.
    /// </summary>
    /// <value>
    /// The order position.
    /// </value>
    [Required]
    [DataMember]
    public int OrderPosition { get; set; }

    /// <summary>
    /// Gets or sets the child of.
    /// </summary>
    /// <value>
    /// The child of.
    /// </value>
    [DataMember]
    public Guid? ChildOf { get; set; }

    /// <summary>
    /// Gets or sets the work flow instance identifier.
    /// </summary>
    /// <value>
    /// The work flow instance identifier.
    /// </value>
    [DataMember]
    public Guid WorkFlowInstanceId { get; set; } 


    /// <summary>
    /// Gets or sets the phase document.
    /// </summary>
    /// <value>
    /// The phase document.
    /// </value>
    public virtual PhaseDocument PhaseDocument { get; set; }

    /// <summary>
    /// Gets or sets the phase pages.
    /// </summary>
    /// <value>
    /// The phase pages.
    /// </value>
    public virtual ICollection<PhasePage>  PhasePages { get; set; }
    /// <summary>
    /// Gets or sets the work flow instance.
    /// </summary>
    /// <value>
    /// The work flow instance.
    /// </value>
    public virtual WorkFlowInstance WorkFlowInstance { get; set; }

}
 public class PhasePage :BaseModelEntity
{
    /// <summary>
    /// Gets or sets the phase page identifier.
    /// </summary>
    /// <value>
    /// The phase page identifier.
    /// </value>
    [Key]
    [Required]
    [DataMember]
    public Guid PhasePageId { get; set; }

    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    /// <value>
    /// The description.
    /// </value>
    [Required]
    [DataMember]
    public string Description { get; set; }

    /// <summary>
    /// Gets or sets the phase module identifier.
    /// </summary>
    /// <value>
    /// The phase module identifier.
    /// </value>
    [Required]
    [DataMember]
    public Guid PhaseModuleId { get; set; }

    /// <summary>
    /// Gets or sets the work flow instance identifier.
    /// </summary>
    /// <value>
    /// The work flow instance identifier.
    /// </value>
    [DataMember]
    public Guid WorkFlowInstanceId { get; set; } 

    /// <summary>
    /// Gets or sets the HTML code.
    /// </summary>
    /// <value>
    /// The HTML code.
    /// </value>
    [Required]
    [DataMember]
    public string HtmlCode { get; set; }

    /// <summary>
    /// Gets or sets the order position.
    /// </summary>
    /// <value>
    /// The order position.
    /// </value>
    [Required]
    [DataMember]
    public int OrderPosition { get; set; }

    /// <summary>
    /// Gets or sets the child of.
    /// </summary>
    /// <value>
    /// The child of.
    /// </value>
    [Required]
    [DataMember]
    public Guid? ChildOf { get; set; }

    /// <summary>
    /// Gets or sets the fields.
    /// </summary>
    /// <value>
    /// The fields.
    /// </value>
    public virtual ICollection<Field> Fields { get; set; }

    /// <summary>
    /// Gets or sets the phase module.
    /// </summary>
    /// <value>
    /// The phase module.
    /// </value>
    public virtual PhaseModule PhaseModule { get; set; }


    /// <summary>
    /// Gets or sets the work flow instance.
    /// </summary>
    /// <value>
    /// The work flow instance.
    /// </value>
    public virtual WorkFlowInstance WorkFlowInstance { get; set; }
}

阶段文档可以包含一个或多个阶段模块。 相位模块可以具有一个或多个相位页面。 所有人都可以拥有WorkFlowInstance,

我的ef配置类是:

  public PhaseDocumentConfiguration(string schema = "dbo")
    {
        ToTable(schema + ".PhaseDocuments");
        HasKey(x => x.PhaseDocumentId);

        Property(x => x.PhaseDocumentId).HasColumnName("PhaseDocumentId").IsRequired();
        Property(x => x.Description).HasColumnName("Description").IsRequired().HasMaxLength(128);
        Property(x => x.Version).HasColumnName("Version").IsRequired();
        Property(x => x.HtmlCode).HasColumnName("HtmlCode").IsRequired();
        Property(x => x.AttachmentsId).HasColumnName("AttachmentsId").IsRequired();
        Property(x => x.ChildOf).HasColumnName("ChildOf").IsOptional();


        //BaseModelEntityProp
        Property(x => x.CreatedBy).HasColumnName("CreatedBy").IsRequired().HasMaxLength(128);
        Property(x => x.CreatedOn).HasColumnName("CreatedOn").IsRequired();
        Property(x => x.ModifiedBy).HasColumnName("ModifiedBy").IsRequired().HasMaxLength(128);
        Property(x => x.LastModify).HasColumnName("LastModify").IsRequired();
        Property(x => x.IsActive).HasColumnName("IsActive").IsRequired();
        Property(x => x.IsDeleted).HasColumnName("IsDeleted").IsRequired();

        // Foreign keys
        HasRequired(a => a.CreatedByUser)
            .WithMany(b => b.PhaseDocument_CreatedBy)
            .HasForeignKey(c => c.CreatedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_CreatedByUser
        HasRequired(a => a.ModifiedByUser)
            .WithMany(b => b.PhaseDocument_ModifiedBy)
            .HasForeignKey(c => c.ModifiedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_ModifiedByUser

        HasRequired(a => a.Attachments)
            .WithMany(r=>r.PhaseDocuments)
            .HasForeignKey(c => c.AttachmentsId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.DynamicPhaseHeader)
            .WithMany(r => r.PhaseDocuments)
            .HasForeignKey(c => c.DynamicPhaseHeaderId)
            .WillCascadeOnDelete(false);
        HasRequired(a => a.WorkFlowInstance)
           .WithMany(r => r.PhaseDocument)
           .HasForeignKey(c => c.WorkFlowInstanceId)
           .WillCascadeOnDelete(false);

    }
public PhaseModuleConfiguration(string schema = "dbo")
    {
        ToTable(schema + ".PhaseModules");
        HasKey(x => x.PhaseModuleId);

        Property(x => x.PhaseModuleId).HasColumnName("PhaseModuleId").IsRequired();
        Property(x => x.Description).HasColumnName("Description").IsRequired().HasMaxLength(128);
        Property(x => x.Version).HasColumnName("Version").IsRequired();
        Property(x => x.PhaseDocumentId).HasColumnName("PhaseDocumentId").IsRequired();
        Property(x => x.HtmlCode).HasColumnName("HtmlCode").IsRequired();
        Property(x => x.OrderPosition).HasColumnName("OrderPosition").IsRequired();
        Property(x => x.ChildOf).HasColumnName("ChildOf").IsOptional();

        //BaseModelEntityProp
        Property(x => x.CreatedBy).HasColumnName("CreatedBy").IsRequired().HasMaxLength(128);
        Property(x => x.CreatedOn).HasColumnName("CreatedOn").IsRequired();
        Property(x => x.ModifiedBy).HasColumnName("ModifiedBy").IsRequired().HasMaxLength(128);
        Property(x => x.LastModify).HasColumnName("LastModify").IsRequired();
        Property(x => x.IsActive).HasColumnName("IsActive").IsRequired();
        Property(x => x.IsDeleted).HasColumnName("IsDeleted").IsRequired();

        // Foreign keys
        HasRequired(a => a.CreatedByUser)
            .WithMany(b => b.PhaseModule_CreatedBy)
            .HasForeignKey(c => c.CreatedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_CreatedByUser
        HasRequired(a => a.ModifiedByUser)
            .WithMany(b => b.PhaseModule_ModifiedBy)
            .HasForeignKey(c => c.ModifiedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_ModifiedByUser

        HasRequired(a => a.PhaseDocument)
            .WithMany(r=>r.PhaseModules)
            .HasForeignKey(c => c.PhaseDocumentId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.WorkFlowInstance)
           .WithMany(r => r.PhaseModule)
           .HasForeignKey(c => c.WorkFlowInstanceId)
           .WillCascadeOnDelete(false);

    }
 public PhasePageConfiguration(string schema = "dbo")
    {
        ToTable(schema + ".PhasePages");
        HasKey(x => x.PhasePageId);

        Property(x => x.PhasePageId).HasColumnName("WardId").IsRequired();
        Property(x => x.Description).HasColumnName("Description").IsRequired().HasMaxLength(128);
        Property(x => x.PhaseModuleId).HasColumnName("PhaseModuleId").IsRequired();
        Property(x => x.HtmlCode).HasColumnName("HtmlCode").IsRequired();
        Property(x => x.OrderPosition).HasColumnName("OrderPosition").IsRequired();
        Property(x => x.ChildOf).HasColumnName("ChildOf").IsOptional();

        //BaseModelEntityProp
        Property(x => x.CreatedBy).HasColumnName("CreatedBy").IsRequired().HasMaxLength(128);
        Property(x => x.CreatedOn).HasColumnName("CreatedOn").IsRequired();
        Property(x => x.ModifiedBy).HasColumnName("ModifiedBy").IsRequired().HasMaxLength(128);
        Property(x => x.LastModify).HasColumnName("LastModify").IsRequired();
        Property(x => x.IsActive).HasColumnName("IsActive").IsRequired();
        Property(x => x.IsDeleted).HasColumnName("IsDeleted").IsRequired();

        // Foreign keys
        HasRequired(a => a.CreatedByUser)
            .WithMany(b => b.PhasePage_CreatedBy)
            .HasForeignKey(c => c.CreatedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_CreatedByUser
        HasRequired(a => a.ModifiedByUser)
            .WithMany(b => b.PhasePage_ModifiedBy)
            .HasForeignKey(c => c.ModifiedBy)
            .WillCascadeOnDelete(false); // FK_XXXX_ModifiedByUser

        HasRequired(a => a.PhaseModule)
            .WithMany(r=>r.PhasePages)
            .HasForeignKey(c => c.PhaseModuleId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.WorkFlowInstance)
           .WithMany(r => r.PhasePage)
           .HasForeignKey(c => c.WorkFlowInstanceId)
           .WillCascadeOnDelete(false);
    }

现在如果我运行应用程序我

  应用程序中的服务器错误。

在表'PhaseModules'上引入FOREIGN KEY约束'FK_dbo.PhaseModules_dbo.WorkFlowInstances_WorkFlowInstanceId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束。查看以前的错误。

描述:执行当前Web请求期间发生了未处理的异常。请

我已经添加了.WillCascadeOnDelete(false);但没有改变。

我错过了什么? TNX!

0 个答案:

没有答案