Giter VIP home page Giter VIP logo

josephlugo / efdynamicconnectionstring Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 489 KB

A template on setting dynamic connection string for Entity Framework data access projects.

License: MIT License

ASP 1.41% Visual Basic 18.45% CSS 0.42% HTML 2.33% JavaScript 77.39%
entity-framework vb-net asp-net webforms visual-studio visual-basic-net visual-basic-for-applications dynamic-connection-string database

efdynamicconnectionstring's Introduction

Entity Framework Dynamic Connection String

A template on setting dynamic connection string for Entity Framework data access projects.

Instructions for getting the solution to work:

1- Restore a backup of WideWorldImporters-Full.bak to your local SQL Server machine. See: https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0

2- Create a new ASP.NET web application project.

3- Right click on top of the created project, Add-> New Item.

4- Select “Data” from the left corner of the dialog.

5- Click on “ADO.NET Entity Data Model”.

6- Specify a name and click “Add”. This will prompt the next window.

7- As the model we want to map already exists in database, select the first option “EF Designer from database”, and click “Next”. A screen prompting for establishing a new connection pops up. Click on New Connection, complete the parameters as you usually do, and establish a connection to the WideWorldImporters database.

8- Once the connection is established, click “OK”. This will bring you back to the previous screen.

9- Set a name for saving the connection settings in Web.config file. Make sure writing down well this name since we will use it later as reference to change the connection string property dynamically. Click “Next”.

10- Select the objects from the database you want to be present in your entity framework model. Once you select them all, specify a Model Namespace name and click on “Finish”. Read all dialogs during the creation of the EF model and accept them properly.

At this point, you must be able to see at the designer, all the selected objects mapped, except the stored procedures (if selected any). This is fine, stored procedures won’t show up here but you must be able to see them by clicking on “Model Browser” tab.

If you go back to the Solution Explorer tab and click on any empty space into the created mapped EF designer, you will be able to see the corresponding properties.

Notice the name for Entity Container is the same one that we used to save the connection settings name in Step 9.*

The Entity Container is the public class created by Entity Framework to call all the objects mapped into its designer (tables, stored procedures, etc.)

14- Go to Web.config file. You will notice how EF have created a corresponding node for the connection string resulting from the earlier configuration wizard.

15- Since we do not want to read that sensitive data from this file, but from another source dynamically, delete that entry from the Web.config.

16- On Solution Explorer, right click on the project node, Add-> New Class.

17- Provide for the new class the same name as the one shown for the Entity Container resulting from Step 13.

18- Since it is not advisable making changes to Entity Framework auto-generated classes, create a new class that extends from DbContext and implements a constructor for passing the connection string as a parameter. For doing that, we are going to build a new class with a source code similar to the one at /EFDynamicConnectionString/WideWorldImportersEntities.vb

19- You are all set! Use the EF dynamic connection string as shown on the sample solution at: https://github.com/jlugogarcia/EFDynamicConnectionString

efdynamicconnectionstring's People

Contributors

josephlugo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.