ASP.NET MVC EF无法确定关联的主要终点 [英] ASP.NET MVC EF Unable to determine the principal end of association
问题描述
在使用实体框架时,我在关联方面遇到了一些问题。虽然还有其他一些帖子我无法使其正常工作。
I've encountered some problems with associations while using Entity Framework. Althought there are some other posts about that I couldn't make it working.
public class BaseJobOffer : IEntity
{
[Key, ForeignKey("File")]
public int Id { get; set; }
public string Name { get; set; }
public int FileId { get; set; }
public File File { get; set; }
}
public class File
{
public int Id { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public Enums.FileType FileType { get; set; }
public int JobOfferId { get; set; }
public virtual BaseJobOffer JobOffer { get; set; }
}
错误提示:
选择运行时出错代码生成器:
And the error says: There was an error running selected code generator:
无法检索Model.JobOffer的元数据。无法确定
类型 Model.BaseJobOffer和 Model.File之间的关联的主要终点。
关联的主体必须使用
关系流利的API或数据注释进行显式配置。'
'Unable to retrieve metadata for Model.JobOffer'. Unable to determine the principal end of association between the types 'Model.BaseJobOffer' and 'Model.File'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.'
推荐答案
请参阅这篇有关ForeignKey属性用法的文章 http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
See this article about ForeignKey attribute usage http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
ForeignKey
属性应位于 ForeignKey
属性或 ForeignKeyID
属性
The ForeignKey
attribute should be on either the ForeignKey
property or the ForeignKeyID
property
public class BaseJobOffer : IEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("File")
public int FileId { get; set; }
//or
[ForeignKey("FileId")
public File File { get; set; }
}
这篇关于ASP.NET MVC EF无法确定关联的主要终点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!