locked
Detach Database RRS feed

  • Question

  • We have an app that was built using EF 5.0 dbContext Code First. We would like the app to upload data to Azure using Sync Framework 2.1. It appears that Sync Framework does not support entities so we believe that we will have to detach the database and re-attach outside of EF to do the sync. We're having a problem detaching the database in code. We've tried closing the connection and disposing the context but when we try to attach for upload we get an exception that the database is already attached. Any ideas on how to detach? Any other thoughts on using Sync Framework with entities?


    Donald Hofferber

    Wednesday, March 20, 2013 9:24 PM

Answers

  • can you post your connection string on both EF and Sync Fx and how you're connecting from EF.
    • Edited by JuneT Wednesday, March 20, 2013 11:53 PM
    • Marked as answer by DDHSolutions Thursday, March 21, 2013 7:49 PM
    Wednesday, March 20, 2013 11:50 PM

All replies

  • i don't think its necessary to detach the database. You just have to connect to it directly without using EF.
    Wednesday, March 20, 2013 11:42 PM
  • Thanks June.  When I do that I get an exception that the database is already attached.  I thought that it might be the old attached in SSMS, which it was, but detaching there didn't help.  Is there a way to retrieve database connections in code?  If so,  I could at least find out who has it attached.

    Donald Hofferber

    Wednesday, March 20, 2013 11:48 PM
  • can you post your connection string on both EF and Sync Fx and how you're connecting from EF.
    • Edited by JuneT Wednesday, March 20, 2013 11:53 PM
    • Marked as answer by DDHSolutions Thursday, March 21, 2013 7:49 PM
    Wednesday, March 20, 2013 11:50 PM
  • EF:  App.config

    <connectionStrings>

    <add name="IPHCRemoteDataEntry" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=IPHCRemoteDataEntry;Integrated Security=True;AttachDBFilename=|DataDirectory|\IPHC Remote Data Entry\Data\IPHCRemoteDataEntry.mdf" providerName="System.Data.SqlClient" />

    </connectionStrings>

    sfx: code uses same connection

    Private Shared ReadOnly SqlLocalConnectionString As String = My.Settings.IPHCRemoteDataEntryLocalConnectionString

    Dim sqlLocalConn As New SqlConnection(SqlLocalConnectionString)

    Exception on:

    Dim provisionLocal As New SqlSyncScopeProvisioning(sqlLocalConn)


    Donald Hofferber


    Wednesday, March 20, 2013 11:57 PM
  • You are on the right track.  I just checked the settings file for the connection string and I had not added |DataDirectory| to that one so it was pointing to a different db.  Still curious why I got an already attached exception.  After fixing I now get a new exceptiopn:

    {"Snapshot isolation transaction failed accessing database 'IPHCRemoteDataEntry' because snapshot isolation is not allowed in this database. Use ALTER DATABASE to allow snapshot isolation."}

    I'll try allowing snapshot isolation and get back.


    Donald Hofferber

    Thursday, March 21, 2013 12:12 AM
  • That did it.  Sync to Azure is now up and running.  The excellent examples that you (June T) point to on your blog should be updated to include setting the snapshot isolation on both db's.  It could already be there and I just missed it.  One last question, if you are tracking this June, is that after we upload to Azure we would like to delete our local data for security reasons.  If we get no errors during sync can we be assured enough that the data was uploaded to delete it in our local db?  Should we validate some other way before deleting?  Thoughts?  And thanks again for getting me going again.

    Donald Hofferber

    Thursday, March 21, 2013 7:48 PM