locked
Issue: Connect to different database based on QueryString parameter using DynamicData RRS feed

Answers

  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 4:38 PM

All replies

  • User-718146471 posted

    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.

    Wednesday, January 8, 2014 2:01 PM
  • User-123809005 posted

    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 via querystring.

    Now I need to figure out how to connect to correct db based on querystring

    Wednesday, January 8, 2014 3:48 PM
  • User-330204900 posted

    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?

    Wednesday, January 8, 2014 6:51 PM
  • User-718146471 posted

    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.

    Thursday, January 9, 2014 7:02 AM
  • User-330204900 posted

    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 :)

    Thursday, January 9, 2014 7:14 AM
  • User-718146471 posted

    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.

    Thursday, January 9, 2014 7:24 AM
  • User-330204900 posted

    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 :)

    Thursday, January 9, 2014 8:08 AM
  • User-718146471 posted

    Do you have any examples? I'd like to try my hand at this.

    Thursday, January 9, 2014 8:14 AM
  • User-330204900 posted

    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

    Thursday, January 9, 2014 9:30 AM
  • User-330204900 posted

    I foudn them they are well hidden :)

    see 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

    Thursday, January 9, 2014 9:37 AM
  • User-123809005 posted

    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?

    Thanks,

    Milap

    Thursday, January 9, 2014 1:51 PM
  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 4:38 PM
  • User-123809005 posted

    Hi Stefan,

    You are correct. This worked.

    Friday, January 10, 2014 7:38 AM