none
refreshing a table STRUCTURE in the datacontext - linq to SQL RRS feed

  • Question

  • I'm use Linq to SQL to perform queries on a TBL_Customer (inside 'MyDataContext' Datacontext).
    i want to refresh the STRUCTURE of the table programmatically ( not the data inside the table), for example:

    if i add a new column to TBL_Customer through SQL Server Management Studio, i want my application to handle this change, meaning - i want this new column to be added in the TBL_Customer table in my DataContext (without me needing to open the VS project ,drag the table to the DataContext again and then compile again).

    datasets does that.... it updates with the new columns without recompiling the project.

    is there a way to refresh the dataContext structure progrmatically ?
    so i can perform that everytime the application runs and save the whole 'preparations' everytime i add a column...
    or a way to detect changes in the table when i run the program?

    Thursday, July 9, 2009 5:09 PM

Answers

  • This is impossible to do with something that is strongly typed such as LINQ to SQL or regular object-relational mappers.

    If you look at the problem - we create a class for each type of object mapped from your database and compile it into your application. All references to it are strongly typed as they access the properties on that class and you pass instances of that class around. You cannot change the class at runtime.

    Dataset can do this as there is only one class regardless of what tables you have and the properties are accessed using string-keys which are of course not strongly typed and can easily change at runtime.

    If this is a requirement you would be better using Dataset and LINQ to Dataset's for your queries.

    [)amien
    Friday, July 10, 2009 3:58 PM
    Moderator
  • SQLMetal can write the class from the database but a. You can't redistribute it and b. You couldn't compile your application against it and c. How would you access properties from your application when you don't know if they exist?

    [)amien
    • Marked as answer by rodniko Sunday, July 12, 2009 7:59 PM
    Sunday, July 12, 2009 7:35 PM
    Moderator

All replies

  • This is impossible to do with something that is strongly typed such as LINQ to SQL or regular object-relational mappers.

    If you look at the problem - we create a class for each type of object mapped from your database and compile it into your application. All references to it are strongly typed as they access the properties on that class and you pass instances of that class around. You cannot change the class at runtime.

    Dataset can do this as there is only one class regardless of what tables you have and the properties are accessed using string-keys which are of course not strongly typed and can easily change at runtime.

    If this is a requirement you would be better using Dataset and LINQ to Dataset's for your queries.

    [)amien
    Friday, July 10, 2009 3:58 PM
    Moderator
  • thank you very much ,

    i'm checking SQLMetal... maybe it can help...
    Sunday, July 12, 2009 6:41 PM
  • SQLMetal can write the class from the database but a. You can't redistribute it and b. You couldn't compile your application against it and c. How would you access properties from your application when you don't know if they exist?

    [)amien
    • Marked as answer by rodniko Sunday, July 12, 2009 7:59 PM
    Sunday, July 12, 2009 7:35 PM
    Moderator
  • thank you, i think i'll use datasets after all..
    Sunday, July 12, 2009 7:59 PM
  • hi,

      could you give me some hints/examples how todo it with datasets ...

    best

    Thursday, July 5, 2012 12:33 PM