我对MVC很新,并且在创建两个实体之间的关系时遇到了困难。
酒店实体(观点):
[Table("hotels", Schema = "dbo")]
public class Hotel
{
[Key]
[Display(Name= "Hotel Code")]
public String code { get; set; }
.
.
.
[InverseProperty("Hotel")]
public virtual ICollection<Charge> Charges { get; set; }
收费实体:
[Table("charges", Schema = "dbo")]
public class Charge
{
[Key]
public Int32 id { get; set; }
[Display(Name = "Hotel Code")]
public String hotelCode { get; set; }
[Display(Name = "Criteria")]
public Int32 criteriaID { get; set; }
[Display(Name = "Classification")]
public Int32 chargeClassificationID { get; set; }
.
.
.
[ForeignKey("hotelCode")]
public virtual Hotel Hotel { get; set; }
[ForeignKey("criteriaID")]
public virtual ICollection<CalculatedDebit> CalculatedDebits { get; set; }
CalculatedDebit Entity(calculatedDebits是一个重要的视图):
[Table("calculatedDebits_distinct", Schema = "dbo")]
public class CalculatedDebit
{
[Key]
[Column(Order = 0)]
public Int32 criteriaID { get; set; }
[Column(Order = 1)]
public Int32 classificationID { get; set; }
[Column(Order = 2)]
public string hotelCode { get; set; }
}
当我这样称呼时:
Hotel hotel = db.Hotels.Find(id);
我收到以下查询:
exec sp_executesql N'SELECT
[Extent1].[criteriaID] AS [criteriaID],
[Extent1].[classificationID] AS [classificationID],
[Extent1].[hotelCode] AS [hotelCode]
FROM [dbo].[calculatedDebits_distinct] AS [Extent1]
WHERE [Extent1].[criteriaID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=6709
而不是查询的标准值,我得到了充电值。我的偏好是让WHERE子句使用calculateDebits中的所有三个值(这意味着不需要集合)......但如果不可能,我可以在事后得到我想要/需要的记录。
对我所缺少的任何帮助都很感激。我也尝试使用InverseProperty向ChargelatedDebits添加Charges集合,但是我收到了这个错误:
“TheProgram.Models.CalculatedDebit”类型属性“Charges”上的InversePropertyAttribute无效。 “CalculatedDebit”属性不是相关类型“TheProgram.Models.Charge”上的有效导航属性。确保该属性存在且是有效的引用或集合导航属性。