views:

80

answers:

2

A LinqToSQL Context class is annotated with DatabaseAttribute by default. It is optional however. In any scenario I can think of you would use the connection string to point at a database, so what's the reason for it. Now, I've obviously read docs. which state, you use it to specify a default database if you wish to omit it from the connection string. But... why oh why would you want to do that? any suggestions?

A: 

Hmm.... good question. I can't think of a good use for this. If you wanted to span multiple databases (different data-contexts) from a single connection (which is not a good idea in the first place), it would still be pretty useless, as it would be fixed for all instances of the data-context.

In every sensible case I can think of, the connection defines the database, or it is a non-issue (standalone db files, etc). I wonder if it wasn't just put there because it (the knowledge of the database name) existed, and so it would be obtainable (in theory) in the same way...

Marc Gravell
A: 

Its hard to know what the design team thought at the time.

My guess it is there to allow optional configurations with a default general connection string in an app with multiple databases. This isn't supported built-in so I doubt it. This would allow for something like:

<add name="GeneralConnection" ... />
<add name="GeneralConnection.SomeDataContext" ... />

All the data contexts would be configured to point to the GeneralConnection. Some extra code to retrieve the data context instance, would check if GeneralConnection.ThatDataContext is there, and use the constructor with the connection string in that case.

Its just a guess. I haven't had a similar need, and I don't think I would rely on something like that.

eglasius