我有一个现有的数据库,我在多个上下文项目中使用Entity Framework作为项目的上下文。该数据库包含几个类:
首先使用现有数据库工具中的代码,它创建了以下内容:
[Table("MarketMessage")]
public partial class MarketMessage
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MarketMessage()
{
messageType300 = new HashSet<messageType300>();
messageType300S = new HashSet<messageType300S>();
messageType300W = new HashSet<messageType300W>();
messageType305 = new HashSet<messageType305>();
}
public int MarketMessageID { get; set; }
public DateTime CreatedOn { get; set; }
[Required]
[StringLength(4)]
public string messageType { get; set; }
[Required]
[StringLength(8)]
public string VersionNumber { get; set; }
public DateTime MarketTimestamp { get; set; }
[Required]
[StringLength(35)]
public string TxRefNbr { get; set; }
[Required]
[StringLength(3)]
public string Sender { get; set; }
[Required]
[StringLength(3)]
public string Recipient { get; set; }
[StringLength(10)]
public string alertFlag { get; set; }
[StringLength(50)]
public string fileName { get; set; }
public bool IsDeleted { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<messageType300> messageType300 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<messageType300S> messageType300S { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<messageType300W> messageType300W { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<messageType305> messageType305 { get; set; }
}
public partial class messageType300
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public messageType300()
{
Meter = new HashSet<Meter>();
UsageFactor = new HashSet<UsageFactor>();
}
public int messageType300ID { get; set; }
public int MarketMessageID { get; set; }
[Required]
[StringLength(11)]
public string MPRN { get; set; }
[StringLength(35)]
public string MPBR { get; set; }
[Required]
[StringLength(35)]
public string NetworkReferenceNumber { get; set; }
[Required]
[StringLength(3)]
public string LoadProfileCode { get; set; }
[Required]
[StringLength(4)]
public string DUoSGroup { get; set; }
[Required]
[StringLength(2)]
public string MeterPointStatusCode { get; set; }
public DateTime ReadDate { get; set; }
public virtual MarketMessage MarketMessage { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Meter> Meter { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UsageFactor> UsageFactor { get; set; }
}
[Table("Meter")]
public partial class Meter
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Meter()
{
RegisterLevelInformation = new HashSet<RegisterLevelInformation>();
}
public int MeterID { get; set; }
[StringLength(15)]
public string MeterCategoryCode { get; set; }
[Required]
[StringLength(9)]
public string SerialNumber { get; set; }
[StringLength(3)]
public string MeterLocationCode { get; set; }
public int? MessageType300ID { get; set; }
public int? MessageType300SID { get; set; }
public int? MessageType300WID { get; set; }
public int? MessageType303RID { get; set; }
public int? MessageType305ID { get; set; }
public int? MessageType306ID { get; set; }
public int? MessageType306WID { get; set; }
public int? MessageType307ID { get; set; }
public int? MessageType307WID { get; set; }
public int? MessageType310ID { get; set; }
public int? MessageType310WID { get; set; }
public int? MessageType320ID { get; set; }
public int? MessageType320WID { get; set; }
public int? MessageType341ID { get; set; }
public int? MessageType342ID { get; set; }
public virtual messageType300 messageType300 { get; set; }
public virtual messageType300S messageType300S { get; set; }
public virtual messageType300W messageType300W { get; set; }
public virtual messageType305 messageType305 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RegisterLevelInformation> RegisterLevelInformation { get; set; }
}
[Table("RegisterLevelInformation")]
public partial class RegisterLevelInformation
{
public int RegisterLevelInformationID { get; set; }
[Required]
[StringLength(3)]
public string MeterRegisterSequence { get; set; }
[Required]
[StringLength(10)]
public string TimeslotCode { get; set; }
[Required]
[StringLength(3)]
public string UOM_Code { get; set; }
public decimal MeterMultiplier { get; set; }
public decimal ReadingValue { get; set; }
[Required]
[StringLength(2)]
public string ReadReasonCode { get; set; }
[Required]
[StringLength(2)]
public string ReadTypeCode { get; set; }
public DateTime? PreviousReadDate { get; set; }
public decimal Consumption { get; set; }
public decimal EstimatedConsumption { get; set; }
[StringLength(4)]
public string ReadStatusCode { get; set; }
[StringLength(2)]
public string RegisterTypeCode { get; set; }
[StringLength(1)]
public string PostDecimalDetails { get; set; }
[StringLength(2)]
public string PreDecimalDetails { get; set; }
public int? MeterID { get; set; }
public int? RemovedMeterRegisterID { get; set; }
public int? NewMeterRegisterID { get; set; }
public int? RetainedMeterRegisterID { get; set; }
public virtual Meter Meter { get; set; }
}
如何调用marketmessage和相关的子项,包括messagetype300,meter和register level information data?
在原始数据库中,我可以使用SQL查询将表连接在一起,如下所示:
select
MarketTimestamp, TxRefNbr, Sender, Recipient, MPRN,
NetworkReferenceNumber, LoadProfileCode, DUoSGroup,
MeterPointStatusCode, ReadDate, SerialNumber,
MeterRegisterSequence, TimeslotCode, UOM_Code, MeterMultiplier,
ReadingValue, ReadReasonCode, ReadTypeCode, PreviousReadDate,
Consumption, EstimatedConsumption, ReadStatusCode, RegisterTypeCode
from
marketmessage as a
inner join
messagetype300 as b on a.MarketMessageID = b.MarketMessageID
inner join
Meter as c on b.messageType300ID = c.MessageType300ID
inner join
RegisterLevelInformation as d on c.MeterID = d.MeterID
where
DUoSGroup = 'T021'
and TimeslotCode = 'UNR'
order by
MarketTimestamp desc
我试图通过以下方式加入此项,但我无法从我的视图中访问仪表或寄存器级别信息的属性:
var test = db.MarketMessage.Where(m => m.messageType == "300S")
.Include(m => m.messageType300S.Select(mt => mt.Meter))
.ToList();
我正在尝试以下方法:
<td>
@Html.DisplayFor(modelItem => item.Recipient)
</td>
<td>
@Html.DisplayFor(modelItem => item.alertFlag)
</td>
<td>
@Html.DisplayFor(modelItem => item.fileName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsDeleted)
</td>
<td>
@foreach (var m in item.messageType300S.Select(m => m.Meter).FirstOrDefault())
{
@Html.Display(m.SerialNumber)
}
</td>
显然我真的不想以这种方式迭代仪表虽然这甚至没有显示序列号,尽管它在我调试时被发现而已经失去了正在发生的事情