none
Changing Connection String at Run Time for Entity Framework 5 RRS feed

  • Question

  • I want to be able to specify the connection string for my entity context at run time. I have seen many references to using a context constructer that takes a connection string or a name of a connection string from the App.Config, however the Entity Framework 5 does not support this.

    I have successfully done this in one app where my model contained the database tables by doing this:

    DataWarehouseEntities entities = new DataWarehouseEntities(); entities.Database.Connection.ConnectionString = "Data Source=server; Initial Catalog=DataWarehouse;integrated security=true";

    But I have another application that only accesses the data through stored procedures. The stored procedures return complex types, but they are not entities, and consequently I have no entities in my model. I access the data through business objects, and the business objects call the stored procedures like this:

    var storedProc = entities.InvoiceSelect(invoiceId, fromDate, throughDate);
    
    var map = from s in storedProc
              select new InvoiceBO()
              {
                   InvoiceId = s.invoice_id,
                   InvoiceDate = s.invoice_date,
                   VendorId = s.vendor_id,
                   VendorName = s.vendor_name
              );
    
    return map.ToList<InvoiceBO>();

    So as you might infer, the business objects are not returning single entities. The stored procedures are taking care of the joins and the complex type returned would map to multiple entities. Further, this is largely a data browsing application, with extremely limited updating capability (e.g. the user can only change the status of records but not any actual data values). Since the stored procedures are already in place, and they already return the data that is needed, I'm not looking to completely re-create the model. I just want to leverage the already existing stored procedures.

    So is there a way to change the connection at run time in such a model?

    Thursday, February 14, 2013 5:22 PM

Answers

  • Time to answer my own question, and bring up some "weirdness" about the Framework.

    It turns out that the Database property of the entity either doesn't exist or is not accessible unless you have the EntityFramework reference in your project. In my case, I added the model to my DLL, which added the EntityFramework reference. When I noticed that my WPF project did NOT have the EntityFramework reference, I tried to add it. But it is NOT listed in the Add Reference dialog. It's not System.Data.Entity, that is only part of the framework.

    The only way I have found to add a reference to the EntityFramework is to add a model to the project.

    So I added an empty model to my WPF project, which added the reference, and then I deleted the empty model. Now I can change the connection string as desired.

    • Marked as answer by IntercareIns Thursday, February 14, 2013 7:18 PM
    Thursday, February 14, 2013 7:18 PM

All replies

  • Time to answer my own question, and bring up some "weirdness" about the Framework.

    It turns out that the Database property of the entity either doesn't exist or is not accessible unless you have the EntityFramework reference in your project. In my case, I added the model to my DLL, which added the EntityFramework reference. When I noticed that my WPF project did NOT have the EntityFramework reference, I tried to add it. But it is NOT listed in the Add Reference dialog. It's not System.Data.Entity, that is only part of the framework.

    The only way I have found to add a reference to the EntityFramework is to add a model to the project.

    So I added an empty model to my WPF project, which added the reference, and then I deleted the empty model. Now I can change the connection string as desired.

    • Marked as answer by IntercareIns Thursday, February 14, 2013 7:18 PM
    Thursday, February 14, 2013 7:18 PM
  • Hi Avertyn,

    Welcome to the MSDN forum.

    Glad to know you solve your problem. Thank you for sharing.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 15, 2013 6:40 AM