locked
sql db connection with azure mvc RRS feed

  • Question

  • User-1261921818 posted

    i have table tbl_broker i would like to connect the sql database using azure mvc , please guide me to connect db with dbcontext method

    Friday, July 3, 2015 1:24 PM

Answers

  • User2024324573 posted

    If I am not wrong you are using Azure SQL database and would like to access the database using Entity Framework in you MVC application, right?

    Well, you can follow both Database-First or Model-First approach with Entity Frame work to work with the database.

    If you use Database-First i.e. you have an existing database and you would like to use the tables from the database as models in your MVC. To add the Database to your MVC project, Add new new item to your project and select Data from the template and you will get the following screen:

    Select ADO.NET Entity Data Model and you will see

    After selecting the option from the wizard it will ask for the Database connection string and if the connection is ok then it will generate a metadata connection string in your web.config file which you will be using for database access.

    In the Model-First (Code-First) you need to add the connection string to you DbContext class and add the models to the class. After having done these, you need to migrate the models to the database if you code successfully builds.

    There are 3 commands you need to issue from Power Shell Console

    Enable-Migrations
    Add-Migration Initial
    Update-Database

    These will create the database in your server and ready to use.

    Please see this post how to connect to a database using Code-First approach: http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx

    Hope this will help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 5, 2015 5:22 PM