包含外键ID时的EF错误:多重性在Role中无效

时间:2014-11-01 18:04:07

标签: c# entity-framework ef-code-first entity-framework-6

我使用的是EF Code First,我的数据库模型如下所示:

public abstract class Job
{
    public int Id { get; set; }
    public JobResult Result { get; set; }
}

[Table("RegisterDomainJobs")]
public class RegisterDomainJob : Job
{

}    

public abstract class JobResult
{
    public int Id { get; set; }

    [ForeignKey("Job")]
    public int JobId { get; set; }

    [Required]
    public virtual Job Job { get; set; }
}

[Table("GenericJobResults")]
public class GenericJobResult : JobResult
{
}

但是当我运行 Update-Database 命令时,我收到以下错误:

JobResult_Job_Source: : Multiplicity is not valid in Role 'JobResult_Job_Source' in relationship 'JobResult_Job'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

奇怪的是,这个错误只发生在我保留的时候:

[ForeignKey("Job")]
public int JobId { get; set; }

在我的模特中。

这个问题可能是什么原因?

1 个答案:

答案 0 :(得分:1)

如果您打算在Job和job结果表之间建立一对一的关系,那么根据实体框架指南,您需要拥有JobResult类,如下所示

public abstract class JobResult{
    [Key, ForeignKey("Job")]
public int JobId { get; set; }

[Required]
public virtual Job Job { get; set; }

}

基本上你的Key和Foreign键应该是JobId