Working with SQL Server LocalDB =============================================== By `Rick Anderson`_ The ``ApplicationDbContext`` class handles the task of connecting to the database and mapping ``Movie`` objects to database records. The database context is registered with the :doc:`Dependency Injection ` container in the ``ConfigureServices`` method in the *Startup.cs* file: .. literalinclude:: start-mvc/sample/src/MvcMovie/Startup.cs :language: c# :lines: 39-45 :dedent: 8 :emphasize-lines: 5,6 The ASP.NET Core :doc:`Configuration ` system reads the ``ConnectionString``. For local development, it gets the connection string from the *appsettings.json* file: .. literalinclude:: start-mvc/sample/src/MvcMovie/appsettings.json :language: javascript :lines: 1-6 :emphasize-lines: 3 When you deploy the app to a test or production server, you can use an environment variable or another approach to set the connection string to a real SQL Server. See :doc:`Configuration ` . SQL Server Express LocalDB -------------------------------- LocalDB is a lightweight version of the SQL Server Express Database Engine that is targeted for program development. LocalDB starts on demand and runs in user mode, so there is no complex configuration. By default, LocalDB database creates "\*.mdf" files in the *C:/Users/* directory. - From the **View** menu, open **SQL Server Object Explorer** (SSOX). .. image:: working-with-sql/_static/ssox.png - Right click on the ``Movie`` table **> View Designer** .. image:: working-with-sql/_static/design.png .. image:: working-with-sql/_static/dv.png Note the key icon next to ``ID``. By default, EF will make a property named ``ID`` the primary key. .. comment: add this when we have it for MVC 6: For more information on EF and MVC, see Tom Dykstra's excellent tutorial on MVC and EF. - Right click on the ``Movie`` table **> View Data** .. image:: working-with-sql/_static/ssox2.png .. image:: working-with-sql/_static/vd22.png Seed the database -------------------------- Create a new class named ``SeedData`` in the *Models* folder. Replace the generated code with the following: .. literalinclude:: start-mvc/sample/src/MvcMovie/Models/SeedData.cs :language: c# :lines: 3-62 Notice if there are any movies in the DB, the seed initializer returns. .. literalinclude:: start-mvc/sample/src/MvcMovie/Models/SeedData.cs :language: c# :lines: 18-21 :dedent: 12 :emphasize-lines: 3 Add the seed initializer to the end of the ``Configure`` method in the *Startup.cs* file: .. literalinclude:: start-mvc/sample/src/MvcMovie/Startup.cs :language: c# :lines: 80-88 :dedent: 8 :emphasize-lines: 8 Test the app - Delete all the records in the DB. You can do this with the delete links in the browser or from SSOX. - Force the app to initialize (call the methods in the ``Startup`` class) so the seed method runs. To force initialization, IIS Express must be stopped and restarted. You can do this with any of the following approaches: .. comment this no longer works - ^F5 (Hold down the control and Shift keys and tap F5) - Right click the IIS Express system tray icon in the notification area and tap **Exit** or **Stop* Site* - .. image:: working-with-sql/_static/iisExIcon.png - .. image:: working-with-sql/_static/stopIIS.png - If you were running VS in non-debug mode, press F5 to run in debug mode - If you were running VS in debug mode, stop the debugger and press ^F5 .. Note:: If the database doesn't initialize, put a break point on the line ``if (context.Movie.Any())`` and start debugging. .. image:: working-with-sql/_static/dbg.png The app shows the seeded data. .. image:: working-with-sql/_static/m55.png