在迁移过程中没有发现实体框架外键 [英] Entity Framework foreign key not found during migration
问题描述
我添加键和外键,我的数据模型后设置迁移时遇到意外的错误。我使用VS2013前preSS,使用.NET框架4.5。
I'm experiencing an unexpected error when setting up a migration after adding keys and foreign keys to my data model. I'm using VS2013 Express, with .NET framework 4.5.
在创建实体框架数据模型,因为类之间的关系键不是按照约定预期,我使用的数据注释作为的 MS数据开发中心。这里的类code:
When creating a data model for Entity Framework, because the relationship keys between classes aren't what is expected by convention, I'm using data annotations as outlined in the MS Data Developer Center. Here's the class code:
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; }
}
}
然而,当我尝试进行更新模型后执行迁移启用的迁移在包管理器控制台
,我收到以下错误:
这类型ForeignKeyAttribute财产woMachine
BacklogTracker.Models.WorkOrder是无效的。外键名称
machSN不是在依赖型发现
BacklogTracker.Models.WorkOrder。名称值应该是一个逗号
外键的属性名的分隔列表。
The ForeignKeyAttribute on property 'woMachine' on type 'BacklogTracker.Models.WorkOrder' is not valid. The foreign key name 'machSN' was not found on the dependent type 'BacklogTracker.Models.WorkOrder'. The Name value should be a comma separated list of foreign key property names.
为什么我的外键名'machSN
不被发现?
推荐答案
我觉得你在模型中有一些错误。默认code首先约定,预计已经宣布在dependend到底一个外键属性 ForeignKey的
的关系(工作单
)那场比赛与主要终点的主键属性(机
)。这是没有必要,他们具有相同的名称,检查该<一个href=\"http://www.entityframeworktutorial.net/$c$c-first/foreignkey-dataannotations-attribute-in-$c$c-first.aspx\"相对=nofollow>链接。所以,在你的工作单
类声明命名属性 machSN
:
I think you have some errors in your model. Default Code First convention for ForeignKey
relationship expected to have declared a foreign key property in the dependend end (WorkOrder
) that match with primary key property of the principal end (Machine
). It is not necessary that they have the same name, check this link. So, declare a property named machSN
in your WorkOrder
class:
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; }
}
您可以找到 woNotes
和 woSegments
导航属性其他错误。在一个一对多的关系,这一面不声明FK,在另一边,在注意
和段
类,例如:
You can find other errors in the woNotes
and woSegments
navigation properties. In this side of a one-to-many relationship you don't declare a FK, is in the other side, in Note
and Segment
classes, for example:
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;}
}
也删除了段
类的<code> ForeignKey的属性在 segNotes
导航属性出于同样的原因之前解释说。
Delete also in the Segment
class the ForeignKey
attribute over segNotes
navigation property for the same reasons explained before.
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; }
}
这篇关于在迁移过程中没有发现实体框架外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!