Search code examples
asp.netlinqlinq-to-sqlasp.net-mvc-3scaffolding

Asp.net MVC3 with LINQ to SQL on multiple identical tables


I successfully retrieved data from an already populated table of a live database using mvc3 and linq 2 SQL. The table is defined in the DataClasses1.dbml.

Now I have to retrieve data from other tables with the same identical structure of DataClasses1 but from different databases on the same SQL Server( DB1.Customers DB2.Customers ecc), and display them grouped by database name.

1) How can I do that without creating N DataClassesN.dbml ? I guess since it's the same table structure I can avoid doing it.

2) (Optional): How can I automatically retrieve data also from tables of new created databases?

3) (Not relevant): How can I define a strongly type view? Seems I can do it using EF but I cannot do it using LINQ 2 SQL.

  • I already thought of creating a view on the database with all the customers tables, but it seems it's a too heavy view!

  • I have a query that returns all the database names (Select name from master..syttables), is it useful?

Thanks in advance


Solution

  • You just pass a different connection string to the data context when you create it. If the databases are truly identical, including all the foreign key relationships, then just do something like:

    var dc = new DataClasses1(db1connectionstring);
    // Do your display of database 1 data
    var dc2 = new DataClasses1(db2connectionstring);
    // Do your display of database 2 data
    

    I have no idea what you mean by #2. Data doesn't retrieve itself.