Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:54:16+00:00 2026-05-31T18:54:16+00:00

I have designed a database whose MDF file will be copied to remote offices,

  • 0

I have designed a database whose MDF file will be copied to remote offices, so basically I will have different databases wth the same scheme. However, some tables from these databases will have to contain the same data. First I was happy because I knew it was easy to sync them using RowVersion columns in each table, but then I remembered that primary key columns in these tables (columns named “ID”) are also identity columns. So I have no idea on how to synchronize them in way that they are identical. With same IDs and everything. Also I am doing this through Entity Framework, which sits between the SQL Server 2008 R2 Express and .NET Framework 4 WCF Service. Any clues?
Note that this is a one-way sync, remote offices need to replicate these tables from the main database but they are not able to modify them and write changes back.

The original thread was started here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e5f89bac-959c-490a-befc-a80d5aa9a9a5/ but I haven’t come to a solution yet. If you take a look at the thread I linked to, you will see that the proposed solution was to attach records from the main DB context to the client DB context and call “ApplyCurrentValues” method to update the client DB. However I have come to conclusion that it would not work at all due to these reasons:

  1. Different EntityKey values between data from two contexts. You can’t attach a record to a context if that record’s EntityKey doesn’t correspond with the context. To get past this issue I had to convert the object from mainDB to the object from clientDB using AutoMapper and set the EntityKey manually prior to attaching the record to clientDB context.
  2. If you want to add a new record (if one exists in mainDB but not in clientDB) you can’t use Attach. If the record you are trying to attach doesn’t exist in the store, EF will throw the exception back at you.
  3. If you want to add a new record, you must use AddObject, but that implies the EntityKey is generated automatically and you will not have control over the identity column. If you try to set EntityKey manually prior to adding a new record, EF will throw an exception at you.

So, the question is, how can I replicate data from the main DB to the client DB using EntityFramework?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:54:17+00:00Added an answer on May 31, 2026 at 6:54 pm

    We have recently implemented this solution, however our database was simple enough and we had one meta server (we call it meta as it is our server which holds only identities) and we have data servers. We have three data servers doing three way sync. Now originally we only had three servers, but inserting new IDs were problem and we didnt want to use GUID as well because it is not human readable.

    So we introduced concept of IdentityServer (we called it MetaServer), which hosts a simple Web Service and simple database, database consists of Tables with only Identities, Hash and LastUpdate, Hash and LastUpdate are used to validate synchronization.

    For example, following two tables are there on Meta Server,

    Tickets
       TicketID (Primary Key,Identity)
       LastUpdate (DateTime)
       Hash (Hash of Ticket)
    
    Tasks
       TaskID (Primary Key, Identity)
       LastUpdate (DateTime)
       Hash (Hash of Task)
    

    Now Data Servers will contain Tickets as follow,

    Tickets
       TicketID (Primary Key)
       Subject
       Message
       ... 
       ...
    Tasks
       TaskID (Primary Key) 
       Subject
       Message
       ...
       ...
    

    And our Save method on ObjectContext looks like following,

    Task task = new Task();
    task.TaskID = MetaService.GetNewTaskID();
    
    
    ...
    ...
    // following is save method, checking insert or update, as it is used in 
    //synchronization, thats why i wrote it like this
    void SaveTask(Task task){
       Task copy = ObjectContext.Tasks.FirstOrDefault(x=>x.TaskID==task.TaskID);
       if(copy==null){
          copy = new Task();
          ObjectContext.Tasks.AddObject(copy);
       }
       CloneData(task,copy);
       ObjectContext.SaveChanges();
    }
    

    To Perform syncing, I would suggest, add a table like this which will Save every Change in Meta Server (Master Server)

    Changes
      ChangeID
      ChangeType = Insert,Update,Delete
      ChangeTable
      ChangeKey
      ChangeTime
    

    Which then every data server can read from Meta Server and update changes…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a Sql Server database whose schema changes. When this happens, I
I have worked with Databases before; however never designed a Database. For this project
I have designed a mnesia database with 5 different tables. The idea is to
I have designed database tables (normalised, on an MS SQL server) and created a
I have a poorly designed database. One of the most important tables has 11,000+
I have a database designed to hold card data from various trading card games.
I am planning to build a database for a school application. I have designed
I have rewritten an old program and designed a new database for it. I
I have never designed a database/data-model/schema from scratch, especially for a web-application. In some
I have designed a gridview in that one of my template field will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.