EF6代码第1个外键到多个表,错误更新数据库

时间:2015-10-29 18:26:47

标签: c# entity-framework entity-framework-6

我有一个评论表,可能有记录链接到不同的表。例如,第一条记录可以链接到我的提交表,第二行可以链接到我的项目表。我想知道定义这种关系的最佳方式。

以下是我的RequestReview表的示例:

[Table("RequestReview")]
public class RequestReview : BaseEntity
{
    [Key,  DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid RequestReviewId { get; set; }

    public Guid ResourceToReviewId { get; set; } // Foreign key of other tables
    [ForeignKey("ResourceToReviewId")]
    public virtual ProjectSubmission ProjectSubmissionToReview { get; set; }
    [ForeignKey("ResourceToReviewId")]
    public virtual Project ProjectToReview { get; set; }
    //...
}

以下是其他几个表格:

[Table("ProjectSubmission")]
public partial class ProjectSubmission : BaseEntity
{    
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ProjectSubmissionId { get; set; }

    [InverseProperty("ProjectSubmissionToReview")]
    public virtual ICollection<RequestReview> RequestedReviews { get; set; }
    //...
}

[Table("Project")]
public partial class Project: BaseEntity
{    
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ProjectId { get; set; }

    [InverseProperty("ProjectToReview")]
    public virtual ICollection<RequestReview> RequestedReviews { get; set; }
    //...
}

我可以添加迁移,但是当我尝试更新数据库时,我得到了这个:

  

ALTER TABLE语句与FOREIGN KEY约束冲突&#34; FK_dbo.RequestReview_dbo.Project_ResourceToReviewId&#34;。冲突发生在数据库&#34; Mkp&#34;,table&#34; dbo.Project&#34;,column&#39; ProjectId&#39;。

My Migration看起来像这样:

    public override void Up()
    {
        AddForeignKey("dbo.RequestReview", "ResourceToReviewId", "dbo.Project", "ProjectId");
    }

更新:

我也在尝试这个,但它不起作用:

[Table("RequestReview")]
public class RequestReview : BaseEntity
{
    [Key,  DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid RequestReviewId { get; set; }

    public Guid ResourceToReviewId { get; set; } // Foreign key of other tables
    [ForeignKey("ResourceToReviewId")]
    public virtual Resource ResourceToReview { get; set; }
    //...
}

[Table("ProjectSubmission")]
public partial class ProjectSubmission : BaseEntity
{    
    [Key, ForeignKey("Resource"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ProjectSubmissionId { get; set; }
    public virtual Resource Resource { get; set; }

    [InverseProperty("ResourceToReview")]
    public virtual ICollection<RequestReview> RequestedReviews { get; set; }
    //...
}

我无法创建迁移。我收到此消息试图创建迁移:

  

实体类型&#39; PublicationSystem.Model.Project&#39;其中的导航属性“请求评论”&#39;声明不是类型&quot; PublicationSystem.Model.Resource&#39;的基类型。由反向导航属性“ResourceToReview&#39;”

引用

0 个答案:

没有答案