我有一个只有两个外键的实体,我得到一个例外:
列名称LookUpTypId无效
两者都有相同的代码,但只有一个导致异常
public class Term
{
public Term ()
{
RequestTimes = new HashSet<RequestTime>();
}
public decimal Id { get; set; }
public string AcademicYear { get; set; }
public string Semester { get; set; }
public int? NumberOfDays { get; set; }
public bool Active { get; set; }
public bool Reserve { get; set; }
public string Description { get; set; }
public virtual ICollection<RequestTime> RequestTimes { get; set; }
}
public class LookUp
{
public LookUp()
{
RequestTimes = new HashSet<RequestTime>();
}
public int Id { get; set; }
public string Group { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public virtual ICollection<RequestTime> RequestTimes { get; set; }
}
public class RequestTime
{
public decimal Id { get; set; }
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
public int RequestMaxNumber { get; set; }
public virtual int LookUpTypId { get; set; }
public virtual decimal TermId { get; set; }
[ForeignKey("TermId")]
public virtual Term Term { get; set; }
[ForeignKey("LookUpTypId")]
public virtual LookUp LookUp { get; set; }
}
//relationship in RequestTimeMap
this.HasRequired<LookUp>(rt => rt.LookUp)
.WithMany(lk => lk.RequestTimes)
.HasForeignKey(rt => rt.LookUpTypId);
this.HasRequired<Term>(rt => rt.Term)
.WithMany(t => t.RequestTimes)
.HasForeignKey(rt => rt.TermId);
谢谢
答案 0 :(得分:0)
据我所知,ForeignKey属性应该在表示外键的属性中使用。
如果你有一个对象作为一个属性,它应该在它的正上方...除非你还有一个单独的属性为它的Id。在这种情况下,您应该使用Id属性上方的属性。
[ForeignKey("LookUpTypId")]
public virtual int LookUpTypId { get; set; }
[ForeignKey("TermId")]
public virtual decimal TermId { get; set; }
public virtual Term Term { get; set; }
public virtual LookUp LookUp { get; set; }
并且......这个ID是十进制的吗?