none
using Auto-sync in LINQToSQL RRS feed

  • Question

  •  Hello ,

      I read some articles about  using "Auto-sync" , But it is not clear for me .

      could anyone tell me about this property .

      Thanks


    ASk

    Monday, February 27, 2012 8:59 AM

Answers

  • Hi himo;

    To your question, "So the CLR can get the values although I set 'Auto-Sync : Never'. So I don't get what is the benefits of it.", it seems that when the column is set to AutoGeneratedValue in the database the model will not allow you to set the AutoSync attribute to Never, One reason my be that the PK will not get updated after an insert is done. You can see this if the property is set to AutoSync to Never and then open the file ContextName.Designer.cs and look at the attributes set to that column you will note that there is no AutoSync attribute set. Now if you  for example set AutoSync to OnInsert then the AutoSync attribute in the file will be set to OnInsert and the following code will work as expected.

    // Create the DataContext
    DataClasses1DataContext dtx = new DataClasses1DataContext();
    
    // Create a Person entity and set properties
    Person obj = new Person( ) { firstName = "Tom", lastName = "Mikle" };
    
    // Insert it to the data context 
    dtx.Persons.InsertOnSubmit( obj );
    // Update the database.
    dtx.SubmitChanges( );
    // Because it was an insert the Id, Status and FullName will be returned with 
    // the current values
    Console.WriteLine( obj.Status + "    " + obj.FullName );
    
    // Now we modify the same record with a new last name
    obj.lastName = "Smith";
    // Submit the change
    dtx.SubmitChanges( );
    // Note that the lastName field has not been updated but it has been in the database
    Console.WriteLine( obj.Status + "    " + obj.FullName );

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 28, 2012 5:32 PM
  • Hi himo;

    To your question, " about the case "4"  : why it returns value  when insert and item and update  it . So as you see case "3" and case"4" returns the same results.", As I stated in my above post I believe that when you set AutoSync to Never the Framework will not allow you to specify it if the column is a auto generated field in the database because the data context uses the PK field which is auto generated by the db to track the entities in the model and used to update those entities in the database. So in order to make sure that other auto generated files does not cause issues with the PK field they just don't allow it. There may be other reasons as well but as I stated this is my reason why this is happening.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 28, 2012 8:45 PM

All replies

  • Hi himo;

    Can you post a link to the article that talks about Auto-Sync.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 27, 2012 3:14 PM
  • Instructs the common language runtime (CLR) to retrieve the value.

    Specifies if the column is automatically synchronized from the value generated by the database on insert or update commands. Valid values for this tag are OnInsert, Always, and Never."

    Refer the below article

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/dbdc47ef-9e5e-404e-b159-e5a728720abf/
    Monday, February 27, 2012 4:29 PM
  • Hello,

      Thanks for ur reply . please follow me the following steps :

    •  I create a table in SQL Server called  "Person". in this table has the folloing columns :

           * Id  (PK int)  ,  firstName(nvarchar(20)) , lastName(nvarchar(20)) , Status (tinyint  defualtValue=1) , FullName (computed column [firstName+' '+lastName]

    •    I drag the table on ".dbml" file .
    •    In the column "Status" I set  [AutoGeneratedValue = true ,   Auto-Sync = Never] .
    •    In the column "FullName" I set  [AutoGeneratedValue = true ,   Auto-Sync = Never] .
    •    I write the following Code :

                Person obj = new Person() {

                    firstName = Tom ,

                    latName = Mikle

                   } ;

                dtx.Persons.InserOnSubmit(obj);

                dtx.SubmitChanges();

               console.WriteLine(obj.Status+"    "+obj.FullName);

    • When running the code ,  The  console write   " 1   TomeMikle  " .

               So the CLR can get the values although I set 'Auto-Sunc : Never' .

             So I dont get what is the benefits of it .

          Thanks


    ASk

    Tuesday, February 28, 2012 9:38 AM
  • Hi himo;

    To your question, "So the CLR can get the values although I set 'Auto-Sync : Never'. So I don't get what is the benefits of it.", it seems that when the column is set to AutoGeneratedValue in the database the model will not allow you to set the AutoSync attribute to Never, One reason my be that the PK will not get updated after an insert is done. You can see this if the property is set to AutoSync to Never and then open the file ContextName.Designer.cs and look at the attributes set to that column you will note that there is no AutoSync attribute set. Now if you  for example set AutoSync to OnInsert then the AutoSync attribute in the file will be set to OnInsert and the following code will work as expected.

    // Create the DataContext
    DataClasses1DataContext dtx = new DataClasses1DataContext();
    
    // Create a Person entity and set properties
    Person obj = new Person( ) { firstName = "Tom", lastName = "Mikle" };
    
    // Insert it to the data context 
    dtx.Persons.InsertOnSubmit( obj );
    // Update the database.
    dtx.SubmitChanges( );
    // Because it was an insert the Id, Status and FullName will be returned with 
    // the current values
    Console.WriteLine( obj.Status + "    " + obj.FullName );
    
    // Now we modify the same record with a new last name
    obj.lastName = "Smith";
    // Submit the change
    dtx.SubmitChanges( );
    // Note that the lastName field has not been updated but it has been in the database
    Console.WriteLine( obj.Status + "    " + obj.FullName );

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 28, 2012 5:32 PM
  • Hello,

      Thanks for ur reply . it was helpful . 

       I Test ur sample  on (FullName) column .

        1- I set "Auto-Sync=onInsert" And the result is  :

                  Tom Mikle

                   Tom Mikle

     2-   I set "Auto-Sync=onUpdate" And the result is  :

                   [No value ]

                   Tom Smith .

    3 -   I set "Auto-Sync=Alwyas" And the result is  :

                   Tom Mikle

                   Tom Smith

    4 -   I set "Auto-Sync=Never" And the result is  :

                   Tom Mikle

                   Tom Smith

        The case 1 , 2 , 3 in correct and clear for me . 

        about the case "4"  : why it returns value  when insert and item and update  it . So as you see case "3" and case"4" returns the 

        same result . I think when set it to "never" ,  it will not return any values .

        Thanks again


    ASk

    Tuesday, February 28, 2012 7:53 PM
  • Hi himo;

    To your question, " about the case "4"  : why it returns value  when insert and item and update  it . So as you see case "3" and case"4" returns the same results.", As I stated in my above post I believe that when you set AutoSync to Never the Framework will not allow you to specify it if the column is a auto generated field in the database because the data context uses the PK field which is auto generated by the db to track the entities in the model and used to update those entities in the database. So in order to make sure that other auto generated files does not cause issues with the PK field they just don't allow it. There may be other reasons as well but as I stated this is my reason why this is happening.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 28, 2012 8:45 PM