【问题标题】:Entity Framework 0:1 relationship isn't being mapped实体框架 0:1 关系未映射
【发布时间】:2016-08-29 06:26:34
【问题描述】:

我有一个职位模型,我正在尝试将可选的职位调查添加到:

public class Job
{

    [Key]
    public int JobID { get; set; }

    // Leaving out all the other fields...

    public virtual JobSurvey JobSurvey { get; set; }

}

工作调查模型如下所示:

public class JobSurvey
{
    [Key]
    public int JobSurveyId { get; set; }

    public string CustomerEmail { get; set; }

    [Index]
    [Column(TypeName = "Date")]
    public DateTime? SentDate { get; set; }

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

    public int? RatingValue { get; set; }

    public virtual Job Job { get; set; }
}

在我的上下文中,我添加了以下内容:

modelBuilder.Entity<Job>()
            .HasOptional(s => s.JobSurvey)
            .WithRequired(j => j.Job);

当我运行 add-migration 时,脚本创建了以下内容:

CreateTable(
    "dbo.JobSurveys",
    c => new
        {
            JobSurveyId = c.Int(nullable: false),
            CustomerEmail = c.String(nullable: false, maxLength: 100),
            SentDate = c.DateTime(storeType: "date"),
            RatingValue = c.Int(),
        })
    .PrimaryKey(t => t.JobSurveyId)
    .ForeignKey("dbo.Jobs", t => t.JobSurveyId)

我的问题是创建的表没有能够转到相关实体的外键属性。

因此,在我的 SQL 中,没有 JobSurveyId 属性可以让我获取工作调查,并且我的 JobSurvey 表没有返回到工作的导航属性。所以我可以创建 JobSurveys,但它们没有关联。

我做错了什么?

编辑

我尝试如下修改 JobSurvey:

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

    public int JobId { get; set; }

没有成功

编辑 2

还尝试将 [Required] 添加到导航属性,但 add-migration 并未将此视为需要更新的更改:

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

【问题讨论】:

  • İ 认为您没有正确显示您的Job 课程。您在模型内初始化上下文?在构造函数中声明属性?
  • 不,我没有在 Job 构造函数中初始化 jobsurvey 属性(因为 JobSurvey 可以为 null)
  • 请在问题中更正您的Job
  • 我的工作班出了什么问题?什么修正?你有所有相关的属性吗?
  • 对不起,我明白你的意思了,我更新了问题

标签: c# asp.net-mvc entity-framework


【解决方案1】:

由于我必须在评论中写很多,试试这个,根据我回复的链接,但我认为你没有正确地遵循它:

public class JobSurvey
{
    [Key]
    public int JobSurveyId { get; set; }

    public string CustomerEmail { get; set; }

    [Index]
    [Column(TypeName = "Date")]
    public DateTime? SentDate { get; set; }

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

    public int? RatingValue { get; set; }

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

    public virtual Job Job { get; set; }
}

编辑 我不完全确定你想要 0:1 还是 1:1,所以这里有所有可能性(在链接中):

1:1

Entity Framework 1 to 1 relationship using code first. how?

EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

0:1

Is it possible to capture a 0..1 to 0..1 relationship in Entity Framework?

Entity Framework 0..1 to 0 relation

基本上这可能是您的解决方案:

public class JobSurvey
{
    [Key, ForeignKey("First")]
    public int JobSurveyId { get; set; }

    public string CustomerEmail { get; set; }

    [Index]
    [Column(TypeName = "Date")]
    public DateTime? SentDate { get; set; }

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

    public int? RatingValue { get; set; }

    public virtual Job Job { get; set; }
}

【讨论】:

  • 尝试这种方式,我在添加迁移时收到以下错误:在模型生成期间检测到一个或多个验证错误:JobSurvey_Job_Source: : Multiplicity is not valid in Role 'JobSurvey_Job_Source' in relationship 'JobSurvey_Job' .因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。
  • 一对一关系中不需要额外的外键。
  • 我还需要fluent API入口吗?
【解决方案2】:

一对一一对零或一关系中主键在依赖模型中也是父模型的外键。不需要额外的外键,因为只有一个数据链接到父级,将它们与他们的ID链接是合理的。因此,在您的情况下,JobSurveyId 内的 JobSurvey 模型也是 Job 的外键。

public class Job
{
    [Key]
    public int JobID { get; set; }

    public virtual JobSurvey JobSurvey { get; set; }
}

public class JobSurvey
{
    [Key]
    public int JobSurveyId { get; set; } // This is also foreign key to Job

    public virtual Job Job { get; set; }
}

请务必查看this site

【讨论】:

  • 是的,您正在寻找外键,我试图向您解释 JobSurveyId 是外键
  • 在我的数据库中,JobSurvey 中没有引用该职位的属性
  • 我不了解您的数据库,但在迁移文件中明确指出:.ForeignKey("dbo.Jobs", t =&gt; t.JobSurveyId)
【解决方案3】:

您的工作类别没问题,您只需将 ForeignKey 属性添加到您的 JobSurvey 到正确的 id 属性:

public class Job
{
    [Key]
    public int JobID { get; set; }

    public virtual JobSurvey JobSurvey { get; set; }
}

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

    /* other properties */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多