【问题标题】:Entity Framework foreign key not found during migration迁移期间未找到实体框架外键
【发布时间】:2015-02-13 02:14:05
【问题描述】:

在向我的数据模型添加键和外键后设置迁移时遇到意外错误。我正在使用带有 .NET 框架 4.5 的 VS2013 Express。

在为实体框架创建数据模型时,因为类之间的关系键不是约定所期望的,所以我使用MS Data Developer Center 中概述的数据注释。这是课程代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BacklogTracker.Models
{
    public class WorkOrder
    {
        [Key]
        public string woNum { get; set; }
        public string woClosingStatus { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Note> woNotes { get; set; }
        [ForeignKey("machSN")]
        public virtual Machine woMachine { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Segment> woSegments { get; set; }
    }

    public class Machine
    {
        [Key]
        public string machSN { get; set; }
        public string machLocation { get; set; }
        public string machModel { get; set; }
    }
    public class Segment
    {
        [Key]
        public int ID { get; set; }
        public uint segNum { get; set; }
        public string segRepair { get; set; }
        [ForeignKey("ID")]
        public virtual ICollection<Note> segNotes { get; set; }
    }
    public class Note
    {
        [Key]
        public int ID { get; set; }
        public DateTime notetimestamp { get; set; }
        public string notestring { get; set; }
    }

}

但是,当我通过在包管理器控制台中执行 enable-migrations 更新模型后尝试执行迁移时,我收到以下错误:

类型上属性“woMachine”的 ForeignKeyAttribute “BacklogTracker.Models.WorkOrder”无效。外键名 在依赖类型上找不到“machSN” 'BacklogTracker.Models.WorkOrder'。名称值应为逗号 外键属性名称的分隔列表。

为什么找不到我的foreign key name 'machSN'

【问题讨论】:

    标签: c# asp.net entity-framework


    【解决方案1】:

    我认为您的模型中有一些错误。 ForeignKey 关系的默认代码优先约定应在依赖端 (WorkOrder) 中声明与主体端 (Machine) 的主键属性匹配的外键属性。它们不必具有相同的名称,请检查此link。所以,在 WorkOrder 类中声明一个名为 machSN 的属性:

    public class WorkOrder
    {
        [Key]
        public string woNum { get; set; }
        public string woClosingStatus { get; set; }
    
        public virtual ICollection<Note> woNotes { get; set; }
    
        public string machSN { get; set; }
        [ForeignKey("machSN")]
        public virtual Machine woMachine { get; set; }
    
        public virtual ICollection<Segment> woSegments { get; set; }
    }
    

    您可以在woNoteswoSegments 导航属性中找到其他错误。在一对多关系的这一侧,您不声明 FK,而在另一侧,在 NoteSegment 类中,例如:

    public class Note
    {
        [Key]
        public int ID { get; set; }
        public DateTime notetimestamp { get; set; }
        public string notestring { get; set; }
    
        [ForeignKey("Order)]
        public string woNum { get; set; }
        public virtual WorkOrder Order{get;set;}
    }
    

    Segment 类中删除ForeignKey 属性而不是segNotes 导航属性,原因与前面解释的相同。

    public class Segment
    {
        [Key]
        public int ID { get; set; }
        public uint segNum { get; set; }
        public string segRepair { get; set; }
        public virtual ICollection<Note> segNotes { get; set; }
    }
    

    【讨论】:

    • 啊,是的,好吧,外键放错了地方。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多