在我的实体框架的重写函数SaveChanges()函数中,我试图序列化原始实体和修改后的实体,但是我很难尝试推断出对象类型是什么,以便我可以将其作为一个XmlSerializer对象的参数。
Public Overrides Function SaveChanges() As Integer
ChangeTracker.DetectChanges()
Dim ctx As ObjectContext = DirectCast(Me, IObjectContextAdapter).ObjectContext
Dim objectStateEntryList As List(Of ObjectStateEntry) = ctx.ObjectStateManager.
GetObjectStateEntries(EntityState.Added Or EntityState.Modified Or EntityState.Deleted).ToList()
For Each Entry As ObjectStateEntry In objectStateEntryList
If Not Entry.IsRelationship Then
Dim objectType As String = Entry.Entity.GetType.Name.Split("_").First
Select Case Entry.State
Case EntityState.Modified
For Each propertyName As String In Entry.GetModifiedProperties()
Dim ser As New XmlSerializer(Entry.Entity.GetType)
Dim sw As New IO.StringWriter
ser.Serialize(sw, Entry.OriginalValues)
Dim serializedObject As String = sw.ToString
Next
End Select
End If
Next
Return MyBase.SaveChanges()
End Function
我尝试了上述内容,但没有用。
答案 0 :(得分:0)
试试这个
Dim originalType = ObjectContext.GetObjectType(Entry.Entity.GetType())
GetObjectType是一种静态方法。
然后将实体对象转换为新类型
Dim convertedEntity = Convert.ChangeType(Entry.Entity, originalType)
Convert.ChangeType也是静态的,存储在System。
中