【发布时间】: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