我正在尝试处理XML文件并将文件的相关属性保存到EF代码第一个数据库,我有以下逻辑:
Process Market Message
Process MessageType342
Process Meter Level
Process Channel Information Level
Process Interval Information Level
为了进入下一步,我需要将实体附加到我的上下文,然后保存更改,因为每个级别都需要从上一级添加外键值,但是当我尝试以下过程时,如下所示下面的代码我收到错误消息已保存第二个MessageType342它出错了以下消息:
已成功提交对数据库的更改,但更新对象上下文时发生错误。 ObjectContext可能处于不一致状态。内部异常消息:发生了引用完整性约束违规:定义引用约束的属性值在关系中的主体和从属对象之间不一致。
我的代码如下:
处理MessageType342
Private Function ProcessMessageType342(ByVal doc As XmlDocument, ByVal marketMessage As MarketMessage)
'Reset the node list to appropriate message type
Dim xmlRootNodeList = doc.GetElementsByTagName("MY_VALUE")
xmlNodeList = doc.GetElementsByTagName("SUBLEVEL")
'Declare Variable for the Non Interval Technical Meter Detailss
Dim MessageType342 = New messageType342
'Declare and Initialize the attributes ExportIntervalMeterDailyReadings
Dim attr1 = ""
Dim attr2 = ""
Dim attr3 = ""
Dim attr4 = ""
Dim attr5 = ""
Dim attr6 = ""
Dim attr7 = ""
'Iterate through the nodes
For i = 0 To xmlNodeList.Count - 1
'Iterate through the possible attributes of MessageType342
For j = 0 To xmlNodeList(i).Attributes.Count - 1
'Set the relevant variable according to the header attributes
Select Case xmlNodeList(i).Attributes(j).Name
Case "attr1" : attr1 = xmlNodeList(i).Attributes(j).Value
Case "attr2" : attr2 = xmlNodeList(i).Attributes(j).Value
Case "attr3" : attr3 = xmlNodeList(i).Attributes(j).Value
Case "attr4" : attr4 = xmlNodeList(i).Attributes(j).Value
Case "attr5" : attr5 = xmlNodeList(i).Attributes(j).Value
Case "attr6" : attr6 = xmlNodeList(i).Attributes(j).Value
Case "attr7" : attr7 = xmlNodeList(i).Attributes(j).Value
End Select
Next
'Set the Mandatory Values for MessageType342
MessageType342.MarketMessageID = marketMessage.MarketMessageID
MessageType342.attr1 = attr1
MessageType342.attr2 = attr2
MessageType342.attr3 = attr3
MessageType342.attr4 = attr4
'Set the Optional Values for MessageType342
If (Not attr5 = "") Then
MessageType342.attr5 = attr5
End If
If (Not attr6 = "") Then
MessageType342.attr6 = attr6
End If
If (Not attr7 = "") Then
MessageType342.attr7 = attr7
End If
'Add MessageType342 class to database context
db.MessageTypes342.Add(MessageType342)
db.SaveChanges()
'Process the Meter for each of the Sub Level Infos
For k = 0 To xmlNodeList(i).ChildNodes.Count - 1
Select Case xmlNodeList(i).ChildNodes(k).Name
Case "MeterID" : ProcessMeters(xmlNodeList(i).ChildNodes(k), marketMessage, MessageType342.messageType342ID)
End Select
Next
Next
'Process the Message Trailers
xmlNodeList = doc.GetElementsByTagName("MessageTrailer")
For i = 0 To xmlNodeList.Count - 1
ProcessMessageTrailers2(xmlNodeList(i), marketMessage, MessageType342.messageType342ID)
Next
Return Nothing
End Function
流程计水平
Private Function ProcessMeters(ByVal node As XmlNode, ByVal marketMessage As MarketMessage, ByVal messageTypeID As Integer)
If node.Attributes.Count = 0 Then
Return Nothing
Else
'Declare Variables
Dim Meter = New Meter
Dim MeterCategoryCode = ""
Dim SerialNumber = ""
Dim MeterLocationCode = ""
For i = 0 To node.Attributes.Count - 1
'Set the relevant variable according to the meter attributes
Select Case node.Attributes(i).Name
Case "MeterCategoryCode" : MeterCategoryCode = node.Attributes(i).Value
Case "SerialNumber" : SerialNumber = node.Attributes(i).Value
Case "MeterLocationCode" : MeterLocationCode = node.Attributes(i).Value
End Select
Next
'Create new CustomerContactDetail entity and set the mandatory values
Meter.SerialNumber = SerialNumber
'Set the optional values
If (Not MeterCategoryCode = "") Then
Meter.MeterCategoryCode = MeterCategoryCode
End If
'Set the optional values
If (Not MeterLocationCode = "") Then
Meter.MeterLocationCode = MeterLocationCode
End If
'Set the relevant message type ID depending on the message type of the header
'Dim mm As MarketMessage = db.MarketMessages.[Single](Function(_mm) _mm.MarketMessageID = marketMessageID)
'Dim mmt As MarketMessageType = db.MarketMessageTypes.[Single](Function(_mmt) _mmt.Header = mm.messageType)
'Select Case mmt.MarketMessageTypeID
Select Case marketMessage.messageType
Case "341" : Meter.MessageType341ID = messageTypeID 'Type341
Case "342" : Meter.MessageType342ID = messageTypeID 'Type342
End Select
'Add Meter class to database context
db.Meters.Add(Meter)
db.SaveChanges()
For j = 0 To node.ChildNodes.Count - 1
Select Case node.ChildNodes(j).Name
Case "RegisterLevelInfo" : ProcessRegisterLevelInfo(node.ChildNodes(j), "Meters", Meter.MeterID)
Case "ChannelInfo" : ProcessChannelInfo(node.ChildNodes(j), "Meters", Meter.MeterID)
Case "SupplierProvidedRead" : ProcessSupplierProvidedReads(node.ChildNodes(j), Meter.MeterID)
End Select
Next
Return Nothing
End If
End Function
处理渠道级别
Private Function ProcessChannelInfo(ByVal node As XmlNode, ByVal meterType As String, ByVal meterID As Integer)
If node.Attributes.Count = 0 Then
Return Nothing
Else
'Declare Mandatory Variables
Dim ChannelInformation = New ChannelInformation
Dim MeteringInterval = ""
Dim RegisterTypeCode = ""
Dim UOM_Code = ""
For i = 0 To node.Attributes.Count - 1
'Set the relevant variable according to the register level information attributes
Select Case node.Attributes(i).Name
Case "MeteringInterval" : MeteringInterval = node.Attributes(i).Value
Case "RegisterTypeCode" : RegisterTypeCode = node.Attributes(i).Value
Case "UOM_Code" : UOM_Code = node.Attributes(i).Value
End Select
Next
'Create new RegisterLevelInformation entity and set the mandatory values
'ChannelInformation.MeterID = CType(meterID, Integer)
ChannelInformation.MeteringInterval = CType(MeteringInterval, Integer)
ChannelInformation.RegisterTypeCode = RegisterTypeCode
ChannelInformation.UOM_Code = UOM_Code
'Set the type of meter to which this belongs
Select Case meterType
Case "Meters" : ChannelInformation.MeterID = meterID
Case "RemovedMeters" : ChannelInformation.RemovedMeterRegisterID = meterID
Case "NewMeters" : ChannelInformation.NewMeterRegisterID = meterID
Case "RetainedMeters" : ChannelInformation.RetainedMeterRegisterID = meterID
End Select
'Add Meter class to database context
db.ChannelInformations.Add(ChannelInformation)
db.SaveChanges()
For j = 0 To node.ChildNodes.Count - 1
Select Case node.ChildNodes(j).Name
Case "IntervalInfo" : ProcessIntervalInfo(node.ChildNodes(j), ChannelInformation.ChannelInformationID)
End Select
Next
Return Nothing
End If
End Function
处理间隔信息级别
Private Function ProcessIntervalInfo(ByVal node As XmlNode, ByVal channelID As Integer)
If node.Attributes.Count = 0 Then
Return Nothing
Else
'Declare Mandatory Variables
Dim IntervalInformation = New IntervalInformation
Dim IntervalValue = ""
Dim IntervalPeriodTimestamp = ""
Dim IntervalStatusCode = ""
Dim NetActiveDemandValue = ""
For i = 0 To node.Attributes.Count - 1
'Set the relevant variable according to the register level information attributes
Select Case node.Attributes(i).Name
Case "IntervalValue" : IntervalValue = node.Attributes(i).Value
Case "IntervalPeriodTimestamp" : IntervalPeriodTimestamp = node.Attributes(i).Value
Case "IntervalStatusCode" : IntervalStatusCode = node.Attributes(i).Value
Case "NetActiveDemandValue" : NetActiveDemandValue = node.Attributes(i).Value
End Select
Next
'Create new RegisterLevelInformation entity and set the mandatory values
IntervalInformation.ChannelInformationID = CType(channelID, Integer)
IntervalInformation.IntervalValue = CType(IntervalValue, Decimal)
IntervalInformation.IntervalPeriodTimestamp = IntervalPeriodTimestamp
IntervalInformation.IntervalStatusCode = IntervalStatusCode
'Set the optional values
If (Not NetActiveDemandValue = "") Then
IntervalInformation.NetActiveDemandValue = CType(NetActiveDemandValue, Decimal)
End If
'Add IntervalInformation class to database context
db.IntervalInformations.Add(IntervalInformation)
db.SaveChanges()
Return Nothing
End If
End Function