none
Dynamic Mapping using two edmx's RRS feed

  • Question

  • Hello,
    In my project i am using two edmx's for two databases. 
    Using a windows service, i need to move the data from first database table to second database table.
    In first table, there are a few columns like X_1,X_2,X_3, .......X_160 for each record. (All columns data need to be moved.)
    In second table, i have added a generic column 'X' and wanted to add 160 columns data in First table as 160 rows in second table.


     
    The exmaple that i have is something like this:

    Dim objFirst As New FirstEntities
    Dim objSecond As New SecondEntities
    Dim firstLinq = (From CD In objFirst.FirstEntityTableName).Distinct
    For Each objFirstLinq In firstLinq
      For i As Integer = 1 To 160
      Dim objSecondEntityTableName  As New SecondEntityTableName
      objSecondEntityTableName.X = "objFirstLinq.X_" & i.ToString
      objSecondEntityTableName.Y = "objFirstLinq.Y_" & i.ToString
      objSecond.AddToSecondEntityTableName(objSecondEntityTableName)
      objSecond.SaveChanges()
      Next
    Next
    Issue: Instead of taking the value of column its storing the string's "objFirstLinq.X_1 to objFirstLinq.X_160" in second table. 
    Need help as how we can type cast the string to take the value without actually passing the column name directly like

    objSecondEntityTableName.X = objFirstLinq.X_1
    objSecond.AddToSecondEntityTableName(objSecondEntityTableName)
    objSecond.SaveChanges()

    objSecondEntityTableName.X = objFirstLinq.X_2
    objSecond.AddToSecondEntityTableName(objSecondEntityTableName)
    objSecond.SaveChanges()
    :
    :
    objSecondEntityTableName.X = objFirstLinq.X_160
    objSecond.AddToSecondEntityTableName(objSecondEntityTableName)
    objSecond.SaveChanges()

    thank you,
    Dileep



    • Edited by Dileep Tuesday, January 3, 2012 9:29 PM
    Tuesday, January 3, 2012 9:26 PM

Answers

  • Hi Dileep,

    Welcome to MSDN Forum.

    Based on the issue, do you mean you want to store 'X_1','X_2','X_3', ... , 'X160' to the second table, not 'objFirstLinq.X_1', 'objFirstLinq.X_2', ... , 'objFirstLinq.X_160'? If I understand correctly, when you create the value of the 'objSecondEntityTableName.X' and 'objSecondEntityTableName.Y', just don't add the 'objFirstLinq' as the prefix. If I misunderstand, please feel free to correct me.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 5, 2012 3:19 AM
    Moderator
  • Hi Allen,

    Sorry for the late response. There is 1-to-1 mapping for those records. I was able to find a fix for the problem. Instead of using a linq query object  'firstLinq', i used a datatable to get the table data.

    Dim dt As DataTable

    Using eCon As New EntityConnection(connectionString)

    eCon.Open()

    Dim myQuery As String = "SELECT VALUE Details FROM FirstEntities.FirstEntityTableName AS Details"

    Using cmd As New EntityCommand(myQuery, eCon)

    dt = DataRepository_DataImport.LoadTable(cmd.ExecuteReader(CommandBehavior.SequentialAccess))

    End Using

    End Using

     

     

    For Each objFirstLinq As Datarow In dt.Rows

       For i As Integer = 1 To 160

       Dim objSecondEntityTableName  As New SecondEntityTableName

       objSecondEntityTableName.X = objFirstLinq(X_" & i.ToString)

       objSecondEntityTableName.Y = objFirstLinq(Y_" & i.ToString)

       objSecond.AddToSecondEntityTableName(objSecondEntityTableName)

       objSecond.SaveChanges()

       Next

    Next

    Monday, January 9, 2012 6:56 PM

All replies

  • Hi Dileep,

    Welcome to MSDN Forum.

    Based on the issue, do you mean you want to store 'X_1','X_2','X_3', ... , 'X160' to the second table, not 'objFirstLinq.X_1', 'objFirstLinq.X_2', ... , 'objFirstLinq.X_160'? If I understand correctly, when you create the value of the 'objSecondEntityTableName.X' and 'objSecondEntityTableName.Y', just don't add the 'objFirstLinq' as the prefix. If I misunderstand, please feel free to correct me.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 5, 2012 3:19 AM
    Moderator
  • Hi Allen,

    Sorry for the late response. There is 1-to-1 mapping for those records. I was able to find a fix for the problem. Instead of using a linq query object  'firstLinq', i used a datatable to get the table data.

    Dim dt As DataTable

    Using eCon As New EntityConnection(connectionString)

    eCon.Open()

    Dim myQuery As String = "SELECT VALUE Details FROM FirstEntities.FirstEntityTableName AS Details"

    Using cmd As New EntityCommand(myQuery, eCon)

    dt = DataRepository_DataImport.LoadTable(cmd.ExecuteReader(CommandBehavior.SequentialAccess))

    End Using

    End Using

     

     

    For Each objFirstLinq As Datarow In dt.Rows

       For i As Integer = 1 To 160

       Dim objSecondEntityTableName  As New SecondEntityTableName

       objSecondEntityTableName.X = objFirstLinq(X_" & i.ToString)

       objSecondEntityTableName.Y = objFirstLinq(Y_" & i.ToString)

       objSecond.AddToSecondEntityTableName(objSecondEntityTableName)

       objSecond.SaveChanges()

       Next

    Next

    Monday, January 9, 2012 6:56 PM
  • Hi Dileep,

    I'm glad to hear that you have solved the issue and thanks for sharing your solution.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 10, 2012 2:29 AM
    Moderator