Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronizing data with identity columns between SQL Express Servers using Entity Framework
    primarykey
    data
    text
    <p>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.</p> <p>The original thread was started here: <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e5f89bac-959c-490a-befc-a80d5aa9a9a5/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e5f89bac-959c-490a-befc-a80d5aa9a9a5/</a> 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:</p> <ol> <li>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.</li> <li>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.</li> <li>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.</li> </ol> <p>So, the question is, how can I replicate data from the main DB to the client DB using EntityFramework?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload