Last post Jan 10, 2014 07:38 AM by milap_great
Jan 08, 2014 01:21 PM|milap_great|LINK
I am currently using asp.net DynamicData with EntityFramework to manage table data via UI. I have a following scenario:
My Page will hae the ClientName in the querystring. For example
All database reside on the same sql instance. Based on the parameter in the querystring I need to connect to the "CodeMapping" Table in the particular client database. I have tried number of options but none of them are working. RegisterRoutes method is
in Application_Start method which only gets called once in the web life cycle with every reset.
So - Could anyone let me know how do I point to the right database depending on the querystring.
Also, Keep in mind that Clients will keep growing so i don't want to put very connectionstring in the web.config as "ClientName" is the DatabaseName.
P.S : Every client has it's own database assigned to them and every database has "CodeMapping" table.
Jan 08, 2014 02:01 PM|bbcompent1|LINK
Ok, let's clarify somehting with the web.config and connection string. That won't need to change since these clients are all on the same DB. I would presume that your codemapping table should actually use a record ID as your primary key, not a name, numeric
is always best practice and easier to maintain. When you pass your Client querystring, you pick it up in the code behind and assign it to a session variable. This way it will maintain through the session until they log out or the session expires. Post your
code and I'll be happy to give you some pointers.
Jan 08, 2014 03:48 PM|milap_great|LINK
Sorry if I was not very clear but every client has its own database assigned to them and it has 100 different tables in each database. CodeMapping is one of those tables and I want to use DynamicData to manage the data in the table. So I am passing clientname
Now I need to figure out how to connect to correct db based on querystring
Jan 08, 2014 06:51 PM|sjnaughton|LINK
Hi you need to look at when the data context is created and change the connection string there, not had a look but you may be able to do that in the entity Data Sources OnContextCreating event?
Jan 09, 2014 07:02 AM|bbcompent1|LINK
Well, I sincerely hope you are connecting in code behind. If so, you merely pass the querystring to the codemapping web page like this:
Session["ConnStr"] = "Server=myServerAddress; Database=" + Request.QueryString("Client").ToString() + "; Trusted_Connection=True;
Doing it this way, you are connecting to the same database server, but this time it sets the connection string as a Session variable. Then in your code, you reference the connection string via the Session Variable.
Jan 09, 2014 07:14 AM|sjnaughton|LINK
Hi bbcompent1, this is Dynamic Data the MetaModel in conjuntion with the EntityDataSource does all that automatically for you so he will have to intercept that in the Context creating event of the Entity Data Source :)
Jan 09, 2014 07:24 AM|bbcompent1|LINK
Ok, with the way he made it sound it seemed like he was referring to using a QueryString passed as a variable. That's the only reason I came up with that solution.
Jan 09, 2014 08:08 AM|sjnaughton|LINK
Yes, I understand, Dynamic Data is confusing until you get an handle on it, Microsoft always pushed for the Unscaffolded version but the power is when it's fully scaffolded and that is what he is doing :)
Jan 09, 2014 08:14 AM|bbcompent1|LINK
Do you have any examples? I'd like to try my hand at this.
Jan 09, 2014 09:30 AM|sjnaughton|LINK
I just uploaded a Dynamic Data Bootstrap Project Template to visual Studio Gallery
here this is a good start you also get the old project template under Web in VS, there are some good tutorials on asp.net/learn see:
there used to be more
Jan 09, 2014 09:37 AM|sjnaughton|LINK
I foudn them they are well hidden :)
http://www.asp.net/web-forms/videos/aspnet-dynamic-data/your-first-scaffold-and-what-is-dynamic-data there is a list of 17 videos down the left hand side
Jan 09, 2014 01:51 PM|milap_great|LINK
Isn't the OnContextCreating event called when we call RegisterContext ?
Currently we call RegisterRoutes in Application_Start and RegisterContext is called inside this method. This method gets only called once during the application lifecycle. So few questions
1. Can I do RegisterContext in Application_Request ?( I tried this and it says duplicate context)
2. Can I associate different model to every request based on querystring?
Jan 09, 2014 04:38 PM|sjnaughton|LINK
OK you can only register context in the ApplicationStart, and OnContextCreating is called from the EntityDataSource on the page when IT creates a context base on the MetaModel. You could change the Connection String in the OnContextCreating event. I have
not tried this but I woudl think it's worth a look.
Jan 10, 2014 07:38 AM|milap_great|LINK
You are correct. This worked.