views:

65

answers:

2

I'm beginning a port of an existing ColdFusion application to .NET MVC and one of the first issues I'm running into is that in ColdFusion we use the fact that you can define multiple datasources and access them in a dynamic way so that a particular user can be pointed at a particular database.

To give an example, I might have two databases, Foo and Bar which each have a table called Locations which store locations particular to that database. The databases are guaranteed to have the same tables, so that's not a concern.

In ColdFusion, you can easily dynamically point a user towards a particular datasource because it's just a string which is configured via the ColdFusion administrator (or you could programatically modify an XML file).

So the question is how to do this in .NET?

And specifically, I think I'd really like to use the Entity framework to leverage the ORM support it'll offer to perform operations on the data in the database, but I'm not sure how to do that (hopefully it's possible). Any thoughts?

+2  A: 

You can easily pass in the connection string to the entity framework's data context constructure.

var context = new MyDataContext(dynamicConnectionString);

This of course assumes that each data source you connect to will have the same exact schema ;-)

Joel Martinez
+1  A: 

When you create your EF model an entry will be added to the Web.config in the configuration section called connectionStrings. You can edit that to point to different databases as well if you don't need to change the database at run-time as Joel suggested.

TheHurt