views:

326

answers:

2

Hi In datasets there was a method WriteXml or ReadXml

Does anyone have any idea on how to this with Entity Framework? Has anyone implemented this before?

A: 

An ObjectContext is nothing like a DataSet. It doesn't make sense to talk about serializing it.

John Saunders
I know, therefore I asked if there is anyone out there who implemented stuff like that, cuz if there ain't i will have to be him
Shimmy
What would you even be serializing?
John Saunders
anything, string, objects everything
Shimmy
A: 

Well basically I wanna have a back up of my database in XML, and I don't want this backup to be handled from the server but from the application.

I think I will handle the event that is fired in the context and then save my data.

As I expext the xml file size not to grow too match (will hold like 100-300 small entity records of simple short data types), I decided to work synchronously against the XML DB:

Here is the partial class of the strongly typed dta set:

Imports System.Xml
Partial NotInheritable Class XmlDatabas : Inherits Data.DataSet
    Private ReadOnly XmlFileName As String = _
            My.Application.Info.DirectoryPath & "\Data.xml"

    Private Sub SeawaveData_Initialized(sender As Object, e EventArgs) _
                Handles Me.Initialized
        If Not IO.File.Exists(XmlFileName) Then WriteXml(XmlFileName)
        ReadXml()
    End Sub

    Public Shadows Sub AcceptChanges()
        MyBase.AcceptChanges()
        WriteXml()
    End Sub

    Public Overloads Sub ReadXml()
        ReadXml(XmlFileName)
    End Sub

BTW, If you bother your self so I even made a quick perfoance test just to get an indication (don't rely, it's a very abstract test, test again if you ever intend to create an XmlDb of larger files!). consider also that this file doesn't test the xml file for external changes, can be good for a win application, in ASP.NET or when you want to allow multi requests, consider a way to implement checking on changes for the existing application, or making a DAL DLL that has a queue of committing changes.

Module Program
    Sub Main()
        Using ds As New XmlDb
            Dim longString = ""
            For index As Integer = 1 To 250
                longString &= "aaaaaaaaaaaaaaaaaaaaaaaaa"
            Next
            Dim step1 = Now
            For index As Integer = 1 To Short.MaxValue
               ds.Account.AddAccountRow(index.ToString, longString, True, True, _
                    longString, "", "", longString, longString, longString, _
                    longString, longString, longString, longString, longString, _
                    longString)
            Next
            Dim step2 = Now
            ds.AcceptChanges()
            Dim step3 = Now
            Console.WriteLine("step 1 took: {0:T}", step2 - step1)
            Console.WriteLine("step 2 took: {0:T}", step3 - step2)
            Console.WriteLine("entire operation took: {0:T}", step3 - step1)
        End Using
        Stop
    End Sub
End Module
    Public Overloads Sub WriteXml()
        WriteXml(XmlFileName)
    End Sub
End Class

Results:
step 1 took: 00:00:01.3390000
step 2 took: 00:00:23.5280000
entire operation took: 00:00:24.8670000

24.8 seconds, big ah? wanna hear how big is the xml file? 2062857146 bytes which is 1967.2938785553 MB

Shimmy
But the ObjectContext is not your database. And will you be deserializing it?
John Saunders
You're right I donno what to say, do you have any good ideas? Maybe in the SavingChanges event I will save the table name, row id, timespan of each commitment to the db, and have a scheduled win-service that backs adds those rows to the xml DB, paralelly, when the server is down i still have a problem. I am basically lookin for a way to make the object context work with two DBs: sql and xml, will they are both synched, if server is down goes to xml then syncs sql with xml, something like that.
Shimmy