tags:

views:

782

answers:

2

We are using Linq To SQL with our own data context logic that executes the one linq query across multiple databases. When we get the results back, we need the database for each of the rows. So...

I want to have a property on my class that will return the database name (SQL Server, so DB_NAME()). How can I do this in Linq To Sql?


Dave, thanks for the answer, but we have hundreds of databases and don't want to have to add views if possible.

+1  A: 

LINQ to SQL allows you to map views and UDFs to objects. Make a UDF view which executes DB_NAME() and returns it as a column, map it using the designer, and you should be good to go.

Dave Markle
+1  A: 

Add this in the manual portion of the partial DataContext class

   [Function(Name = "DB_Name", IsComposable = true)]
    public string GetDBName()
    {
        return ((string)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
    }
David B