问题描述
在运行我的第一个asp.net MVC应用程序,我得到这个错误
我认为,实体框架自动将创造列名与ID的结束键?不是它是否正确?
正如你可以看到ApplicantPositionID将与2列作为主键的表,因为这将涉及到申请人和也的位置。
模型生成过程中检测到一个或多个验证错误:
System.Data.Edm.EdmEntityType:的EntityType'ApplicantImage没有定义键。定义此的EntityType的关键。
System.Data.Edm.EdmEntityType:的EntityType'ApplicationPositionHistory没有定义键。定义此的EntityType的关键。
System.Data.Edm.EdmEntitySet:的EntityType:EntitySet的ApplicantsPositions是基于一个没有定义的键型ApplicantPosition。
System.Data.Edm.EdmEntitySet:的EntityType:EntitySet的ApplicantImages是基于一个没有定义的键型ApplicantImage。
System.Data.Edm.EdmEntitySet:的EntityType:EntitySet的ApplicationsPositionHistory是基于一个没有定义的键型ApplicationPositionHistory。
该错误是在此行中抛出的:
公众的ActionResult指数()
{
返回查看(db.Positions.ToList());
}
和我的模型是以下之一:
命名空间HRRazorForms.Models
{ 公共类职位
{
公众诠释PositionID {搞定;组; }
[StringLength(20,MinimumLength = 3)]
公共字符串名称{;组; }
公众诠释yearsExperienceRequired {搞定;组; }
公共虚拟的ICollection< ApplicantPosition> applicantPosition {搞定;组; }
} 公共类申请人
{
公众诠释ApplicantId {搞定;组; }
[StringLength(20,MinimumLength = 3)]
公共字符串名称{;组; }
公共字符串电话{搞定;组; }
公共字符串skypeuser {搞定;组; }
公共ApplicantImage照片{搞定;组; }
公共虚拟的ICollection< ApplicantPosition> applicantPosition {搞定;组; } } 公共类ApplicantPosition
{
公众诠释ApplicantID {搞定;组; }
公众诠释PositionID {搞定;组; }
公共虚拟范围位置{搞定;组; }
公共虚拟申请人申请{搞定;组; }
公众的DateTime appliedDate {搞定;组; }
公众诠释StatusValue {搞定;组; } 公开状态状态
{
{返回(状态)StatusValue; }
集合{StatusValue =(int)的值; }
} // [NotMapped]
//公众诠释numberOfApplicantsApplied
// {
//获取
// {
// INT查询=
//(美联社位置
//其中ap.Status ==(INT)Status.Applied
//选择AP
//).Count之间的();
//返回查询;
//}
//}
}
公共类ApplicantImage
{
公众诠释ApplicantId {搞定;私人集; }
公众的byte []图片{搞定;组; }
} 公共类地址
{
[StringLength(20,MinimumLength = 3)]
公共字符串国家{搞定;组; }
[StringLength(20,MinimumLength = 3)]
公共字符串城{搞定;组; }
[StringLength(20,MinimumLength = 3)]
公共字符串AddressLine1 {搞定;组; }
公共字符串AddressLine2 {搞定;组; }
} 公共类ApplicationPositionHistory
{
公共ApplicantPosition applicantPosition {搞定;组; }
公共状态OLDSTATUS {搞定;组; }
公共状态NEWSTATUS {搞定;组; }
[StringLength(500,MinimumLength = 10)]
公共字符串意见{搞定;组; }
公众的DateTime dateModified {搞定;组; }
} 公共枚举状态
{
应用,
AcceptedByHR,
AcceptedByTechnicalDepartment,
InterviewedByHR,
InterviewedByTechnicalDepartment,
InterviewedByGeneralManager,
AcceptedByGeneralManager,
不接受
}}
EF code首先只能推断,一个属性是一个主键如果该属性被称为编号
或<类名称>标识
(或者如果它被注释与关键属性)。
所以,你需要延长你如ApplicantImage与ApplicantImageId或id属性等。
编辑:关于coneventions一个条每页:<一href=\"http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-$c$c-first.aspx\">Conventions为code首先
When running my first asp.net mvc application I got this errorI thought that entity framework automatically would create the keys of column names that end with Id? isnt it correct?
As you can see the ApplicantPositionID would be a table with 2 columns as primary key because it would relate to Applicants and also to Position.
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'ApplicantImage' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'ApplicationPositionHistory' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ApplicantsPositions� is based on type �ApplicantPosition� that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ApplicantImages� is based on type �ApplicantImage� that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ApplicationsPositionHistory� is based on type �ApplicationPositionHistory� that has no keys defined.
The error is thrown in this line:
public ActionResult Index()
{
return View(db.Positions.ToList());
}
And my model is the following one:
namespace HRRazorForms.Models
{
public class Position
{
public int PositionID { get; set; }
[StringLength(20, MinimumLength=3)]
public string name { get; set; }
public int yearsExperienceRequired { get; set; }
public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
}
public class Applicant
{
public int ApplicantId { get; set; }
[StringLength(20, MinimumLength = 3)]
public string name { get; set; }
public string telephone { get; set; }
public string skypeuser { get; set; }
public ApplicantImage photo { get; set; }
public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
}
public class ApplicantPosition
{
public int ApplicantID { get; set; }
public int PositionID { get; set; }
public virtual Position Position { get; set; }
public virtual Applicant Applicant { get; set; }
public DateTime appliedDate { get; set; }
public int StatusValue { get; set; }
public Status Status
{
get { return (Status)StatusValue; }
set { StatusValue = (int)value; }
}
//[NotMapped]
//public int numberOfApplicantsApplied
//{
// get
// {
// int query =
// (from ap in Position
// where ap.Status == (int)Status.Applied
// select ap
// ).Count();
// return query;
// }
//}
}
public class ApplicantImage
{
public int ApplicantId { get; private set; }
public byte[] Image { get; set; }
}
public class Address
{
[StringLength(20, MinimumLength = 3)]
public string Country { get; set; }
[StringLength(20, MinimumLength = 3)]
public string City { get; set; }
[StringLength(20, MinimumLength = 3)]
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
public class ApplicationPositionHistory
{
public ApplicantPosition applicantPosition { get; set; }
public Status oldStatus { get; set; }
public Status newStatus { get; set; }
[StringLength(500, MinimumLength = 10)]
public string comments { get; set; }
public DateTime dateModified { get; set; }
}
public enum Status
{
Applied,
AcceptedByHR,
AcceptedByTechnicalDepartment,
InterviewedByHR,
InterviewedByTechnicalDepartment,
InterviewedByGeneralManager,
AcceptedByGeneralManager,
NotAccepted
}
}
EF Code First can only infer that a property is a primary key if the property is called Id
or <class name>Id
(or if it is annotated with the Key attribute).So you need to extend your e.g. ApplicantImage with an ApplicantImageId or Id property etc.
Edit: An artice about the coneventions: Conventions for Code First
这篇关于的EntityType'ApplicantPosition“没有定义键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!