SubSonic Forums
All Your Database Are Belong To Us

How to specify TableName after code generation?

Latest post 10-17-2008 12:37 PM by jrummell. 11 replies.
  • 09-26-2008 2:52 AM

    How to specify TableName after code generation?

    I need to change ActiveRecord's database table after code generation for entegration to existing ERP system.

    For example ERP system has "Invoice" table but it's table name is CCC_PP_INVOICE where CCC = company no, PP = period no.

    So, for Invoice there can be many tables (for different firms and fınancial year) but their structure is SAME.

    I just need to change database table name like

    Invoice.DatabaseTableName ="112_01_INVOICE"

    at runtime after code generation.

     

    Can I do that with superb SupSonic tool?

    Filed under:
  • 09-26-2008 9:10 AM In reply to

    Re: How to specify TableName after code generation?

    Wow... I cant even begin to think about how many WTF's are in that requirement.  Physical tables to represent logical divisions is just wrong.  My DBA brain just exploded. 

    The only way I can see making that work with SubSonic is to run everything through as an inline query.  Of course, that means you lose all the SubSonic goodness.

    Of course, you could take the route of generating a new DAL every period and hit it via reflection and put all the nasty logic required to determine the classes based on company and period in your code.

    Or, you could generate stored procs that take the table as a parameter and use dynamic SQL to query and insert/update the data.

    Or, you could change the SubSonic templates that do the generation to take in a table name on the fly and sling that around.

    Not one of those options is very pretty.  WIthout doing the research required, the last option might be feasible.  It certainly would not be an option for LLBL or nHibernate, thats for sure.

    Software Is Like Cathedrals: First we build 'em, then we pray.
  • 09-26-2008 1:49 PM In reply to

    Re: How to specify TableName after code generation?

    Thanks man.

    I am working with Subsonic for 5 days and I deployed my first application today. But it is for one firm and one period. I have to change it programmatically. Now I am looking for a way to change table name.

  • 09-26-2008 2:49 PM In reply to

    Re: How to specify TableName after code generation?

    I dont envy you, that is a horrible position to be in.

    Software Is Like Cathedrals: First we build 'em, then we pray.
  • 09-27-2008 4:04 AM In reply to

    Re: How to specify TableName after code generation?

     I have crossed a similar bridge before using a view that UNION'd all the tables, but that wont help you if you have to update the tables.

     

  • 10-01-2008 6:35 PM In reply to

    Re: How to specify TableName after code generation?

    I have to update tables.

  • 10-01-2008 7:26 PM In reply to

    Re: How to specify TableName after code generation?

    Its a hack, but if you have enough control over the database, you could create a new table that is exactly the same as the others but add Company and Period columns to it. Have your app update/insert/select this table, then use a trigger to update the appropriate table.

     

     

  • 10-02-2008 11:15 AM In reply to

    Re: How to specify TableName after code generation?

    It would be great if I have several tables but I have many  of them with many columns. So it is not a good solution for me.

  • 10-02-2008 12:45 PM In reply to

    Re: How to specify TableName after code generation?

    I investigate through the souce code and I have found that table's schema property holds table name information which has "TableName" property. With Get and Set methods fortunately.


    I 've created an test app and verify that

    Invoice.Schema.TableName = "002_01_INVOICE"

    works as I expected.

    So, after code generation, if I have to change table name I just read my configuration file and change table name in runtime.

     

  • 10-03-2008 12:47 AM In reply to

    • dbr
    • Top 25 Contributor
    • Joined on 07-07-2007
    • Posts 93

    Re: How to specify TableName after code generation?

    I like your solution and sounds plausible. I worked on a large database for a food manufacturer and there were many tables with duplicate schemas to hold work in progress at different levels so this problem can arise sometimes. I've been scolded before for bad designs on some forums, but they don't know my problem domain and what it requires. I've modified subsonic to handle certain conditions that the designers were probably not considering.

     

     

  • 10-04-2008 3:28 AM In reply to

    Re: How to specify TableName after code generation?

    turkert:

    Invoice.Schema.TableName = "002_01_INVOICE"

     

    Yeah, that would be the perfect solution. I hadn't considered that the TableName might actually be updatable.

     

  • 10-17-2008 12:37 PM In reply to

    Re: How to specify TableName after code generation?

    turkert:

    Invoice.Schema.TableName = "002_01_INVOICE"

    I'm also stuck with a similar schema.  I'm coding against an ERP database that has table names like ttcibd001xxx where xxx is the company number.  I know its ugly, but I can't change the schema.  I have been wanting very badly to use an ORM tool like SubSonic, but I didn't realize changing the table name at runtime was possible.  Thanks for sharing!

     

Page 1 of 1 (12 items) | RSS