<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>entity framework Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/entity-framework/</link>
	<description>Building real-world software with AI 🤖</description>
	<lastBuildDate>Sat, 17 Nov 2018 12:40:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>entity framework Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/entity-framework/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Entity Framework Core &#8211; database initialization with Unit Test</title>
		<link>https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/</link>
					<comments>https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Thu, 27 Jul 2017 19:15:59 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp-net-core]]></category>
		<category><![CDATA[entity framework]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=2331</guid>

					<description><![CDATA[<p>I&#8217;ve recently been presented a concept of initializing the database (creating or re-creating it) with Unit Test method. Initially I thought it&#8217;s a non-sense, but after a while of taking a deeper look&#8230; Code First approach The method of initializing the database I mentioned was used with Entity Framework Core in ASP.NET Core project, where&#8230;</p>
<p>The post <a href="https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/">Entity Framework Core &#8211; database initialization with Unit Test</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve recently been presented a concept of initializing the database (creating or re-creating it) with Unit Test method. Initially I thought it&#8217;s a non-sense, but after a while of taking a deeper look&#8230;<br />
<span id="more-2331"></span></p>
<h1>Code First approach</h1>
<p>The method of initializing the database I mentioned was used with Entity Framework Core in ASP.NET Core project, where Code First database creation approach was used. As you know, this approach implies that we create models (classes) representing our database entities in the code first and then, using an ORM system (such as EF), database structures are created.</p>
<p>&nbsp;</p>
<p>This is very convenient, especially in prototyping. I&#8217;ve developed few small or average-size ASP.NET apps and I always used Code First. However, I cannot say how it works on production as these apps were university or pet projects which I&#8217;ve never deployed on real customer&#8217;s environment. What I noticed is that creating entities using this approach is fast and quite easy.</p>
<h1>Database initialization in development phase</h1>
<p>As long as your project is in development phase, different developers are working on it and there is some database behind, but the data itself is not very important (you only need the database structure &#8211; there&#8217;s no production data in it yet), programmers often need to have the database (re)created. To make this process quick and easy, instead of using <a href="https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations" target="_blank" rel="noopener">Migrations</a> straightaway, you can define your models, <span style="color: #ff9900;">DbContext</span> and write a Unit Test method which initializes the database. Then, each developer working on the project only needs to re-run this Unit Test to have the database created.</p>
<p>What&#8217;s more, as soon as another programmer makes any change in any of the models, the others just need to re-run the Unit Test which re-creates the database and potentially fills it with sample data. There&#8217;s no need to keep any migration files/scripts in the development phase.</p>
<p>The following subsections present how to do that.</p>
<p>Examples are based on a simple ASP.NET Core MVC application called <em>CarServiceMvc</em>. I&#8217;ve used .NET Core 2.0 Preview 2 and Visual Studio 2017 15.3.0 Preview 3.0.</p>
<p>The whole source code <a href="https://github.com/dsibinski/DotNetCoreFun/tree/master/CarServiceMvc" target="_blank" rel="noopener">is available on GitHub</a>.</p>
<h3>Models</h3>
<p>First, let&#8217;s create a simple model in our application. The following class is created in <em>Models</em> folder:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72554742" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-car-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Car.cs content, created by dsibinski on 06:01PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Car.cs">
<tr>
<td id="file-car-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-car-cs-LC1" class="blob-code blob-code-inner js-file-line">  public class Car</td>
</tr>
<tr>
<td id="file-car-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-car-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-car-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-car-cs-LC3" class="blob-code blob-code-inner js-file-line">      public int Id { get; set; }</td>
</tr>
<tr>
<td id="file-car-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-car-cs-LC4" class="blob-code blob-code-inner js-file-line">      public string Producer { get; set; }</td>
</tr>
<tr>
<td id="file-car-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-car-cs-LC5" class="blob-code blob-code-inner js-file-line">      public string Model { get; set; }</td>
</tr>
<tr>
<td id="file-car-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-car-cs-LC6" class="blob-code blob-code-inner js-file-line">      public DateTime ProductionDate { get; set; }</td>
</tr>
<tr>
<td id="file-car-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-car-cs-LC7" class="blob-code blob-code-inner js-file-line">      public bool IsOnWarranty { get; set; }</td>
</tr>
<tr>
<td id="file-car-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-car-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-car-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-car-cs-LC9" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/42de3e4c8ea259bb504939c8a3d24fea/raw/4a46ae9d2f55efd5c963d1b9bf4d5f6ecffd0ff1/Car.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/42de3e4c8ea259bb504939c8a3d24fea#file-car-cs" class="Link--inTextBlock"><br />
          Car.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<h3>DbContext</h3>
<p>Next, let&#8217;s add <span style="color: #ff9900;">CarServiceContext</span> to our application. Let&#8217;s store it in <em>ORM</em> folder.</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72554887" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-carservicecontext-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="CarServiceContext.cs content, created by dsibinski on 06:03PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="CarServiceContext.cs">
<tr>
<td id="file-carservicecontext-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-carservicecontext-cs-LC1" class="blob-code blob-code-inner js-file-line">  public class CarServiceContext : DbContext</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-carservicecontext-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-carservicecontext-cs-LC3" class="blob-code blob-code-inner js-file-line">      // DB connection properties injected automatically</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-carservicecontext-cs-LC4" class="blob-code blob-code-inner js-file-line">      public CarServiceContext() {}</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-carservicecontext-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-carservicecontext-cs-LC6" class="blob-code blob-code-inner js-file-line">      // DB connection properties given explicitly (for Unit Tests)</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-carservicecontext-cs-LC7" class="blob-code blob-code-inner js-file-line">      public CarServiceContext(DbContextOptions options) : base(options) { }</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-carservicecontext-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-carservicecontext-cs-LC9" class="blob-code blob-code-inner js-file-line">      public DbSet&lt;Car&gt; Cars { get; set; }</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-carservicecontext-cs-LC10" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontext-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-carservicecontext-cs-LC11" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/ae4a1fa5c1cf2797ff3f7f6e90d3ca9b/raw/440012a73ba7d2bb210e3185d86dadec207db570/CarServiceContext.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/ae4a1fa5c1cf2797ff3f7f6e90d3ca9b#file-carservicecontext-cs" class="Link--inTextBlock"><br />
          CarServiceContext.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Constructor taking <span style="color: #ff9900;">DbContextOptions</span> has been created for Unit Tests purpose.</p>
<p>We also have a <span style="color: #ff9900;">DbSet</span> of <span style="color: #ff9900;">Cars</span>.</p>
<p>We also need to add a connection string to <em>appsettings.json</em>. For the development phase we can use <em>mssqllocaldb </em>included within Visual Studio:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72555034" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-appsettings-json" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-json  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="appsettings.json content, created by dsibinski on 06:05PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="appsettings.json">
<tr>
<td id="file-appsettings-json-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-appsettings-json-LC1" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-appsettings-json-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-appsettings-json-LC2" class="blob-code blob-code-inner js-file-line">    &quot;Logging&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-appsettings-json-LC3" class="blob-code blob-code-inner js-file-line">      &quot;IncludeScopes&quot;: false,</td>
</tr>
<tr>
<td id="file-appsettings-json-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-appsettings-json-LC4" class="blob-code blob-code-inner js-file-line">      &quot;Debug&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-appsettings-json-LC5" class="blob-code blob-code-inner js-file-line">        &quot;LogLevel&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-appsettings-json-LC6" class="blob-code blob-code-inner js-file-line">          &quot;Default&quot;: &quot;Warning&quot;</td>
</tr>
<tr>
<td id="file-appsettings-json-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-appsettings-json-LC7" class="blob-code blob-code-inner js-file-line">        }</td>
</tr>
<tr>
<td id="file-appsettings-json-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-appsettings-json-LC8" class="blob-code blob-code-inner js-file-line">      },</td>
</tr>
<tr>
<td id="file-appsettings-json-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-appsettings-json-LC9" class="blob-code blob-code-inner js-file-line">      &quot;Console&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-appsettings-json-LC10" class="blob-code blob-code-inner js-file-line">        &quot;LogLevel&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-appsettings-json-LC11" class="blob-code blob-code-inner js-file-line">          &quot;Default&quot;: &quot;Warning&quot;</td>
</tr>
<tr>
<td id="file-appsettings-json-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-appsettings-json-LC12" class="blob-code blob-code-inner js-file-line">        }</td>
</tr>
<tr>
<td id="file-appsettings-json-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-appsettings-json-LC13" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-appsettings-json-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-appsettings-json-LC14" class="blob-code blob-code-inner js-file-line">    },</td>
</tr>
<tr>
<td id="file-appsettings-json-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-appsettings-json-LC15" class="blob-code blob-code-inner js-file-line">    &quot;ConnectionStrings&quot;: {</td>
</tr>
<tr>
<td id="file-appsettings-json-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-appsettings-json-LC16" class="blob-code blob-code-inner js-file-line">      &quot;DefaultConnection&quot;: &quot;Server=(localdb)\\mssqllocaldb;Database=aspnet-Demo;Trusted_Connection=True;MultipleActiveResultSets=true&quot;</td>
</tr>
<tr>
<td id="file-appsettings-json-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-appsettings-json-LC17" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-appsettings-json-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-appsettings-json-LC18" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/cd5658a4b56bc1d3f846ed557fc3ffb6/raw/15151d4f67592c904da85c66fdebbf07747d7e64/appsettings.json" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/cd5658a4b56bc1d3f846ed557fc3ffb6#file-appsettings-json" class="Link--inTextBlock"><br />
          appsettings.json<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>In the end, <em>Startup.cs</em> should be modified to have our <span style="color: #ff9900;">DbContext <span style="color: #000000;">registered:</span></span></p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72555195" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-startup_dbcontext-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Startup_dbContext.cs content, created by dsibinski on 06:07PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Startup_dbContext.cs">
<tr>
<td id="file-startup_dbcontext-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-startup_dbcontext-cs-LC1" class="blob-code blob-code-inner js-file-line">  public void ConfigureServices(IServiceCollection services)</td>
</tr>
<tr>
<td id="file-startup_dbcontext-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-startup_dbcontext-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-startup_dbcontext-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-startup_dbcontext-cs-LC3" class="blob-code blob-code-inner js-file-line">      services.AddDbContext&lt;CarServiceContext&gt;(o =&gt;</td>
</tr>
<tr>
<td id="file-startup_dbcontext-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-startup_dbcontext-cs-LC4" class="blob-code blob-code-inner js-file-line">          o.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));</td>
</tr>
<tr>
<td id="file-startup_dbcontext-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-startup_dbcontext-cs-LC5" class="blob-code blob-code-inner js-file-line">      services.AddMvc();</td>
</tr>
<tr>
<td id="file-startup_dbcontext-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-startup_dbcontext-cs-LC6" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/b30022b5644925bf8423e2026ad86bb2/raw/6c23b8f62800f9e0fd76aabeb04b7d116a7f2a59/Startup_dbContext.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/b30022b5644925bf8423e2026ad86bb2#file-startup_dbcontext-cs" class="Link--inTextBlock"><br />
          Startup_dbContext.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Now we can add a new controller for the <span style="color: #ff9900;">Car</span> model we created, so Visual Studio scaffolds all basic views. However, if we try to launch the application calling one of <span style="color: #ff9900;">CarsController</span>&#8216;s methods, we get the following exception:</p>
<figure id="attachment_2334" aria-describedby="caption-attachment-2334" style="width: 461px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?ssl=1" target="_blank" rel="noopener"><img data-recalc-dims="1" fetchpriority="high" decoding="async" data-attachment-id="2334" data-permalink="https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/carnotfound/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?fit=1065%2C452&amp;ssl=1" data-orig-size="1065,452" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="CarNotFound" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?fit=1024%2C435&amp;ssl=1" class="wp-image-2334" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?resize=461%2C195&#038;ssl=1" alt="" width="461" height="195" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?resize=300%2C127&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?resize=768%2C326&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?resize=1024%2C435&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?resize=720%2C306&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarNotFound.png?w=1065&amp;ssl=1 1065w" sizes="(max-width: 461px) 100vw, 461px" /></a><figcaption id="caption-attachment-2334" class="wp-caption-text">Car not found</figcaption></figure>
<p>So, let&#8217;s see how we can initialize our database with Unit Test.</p>
<h3>Database initialization Unit Test</h3>
<p>Firstly, new project should be added to our solution. Let&#8217;s add a new one of type Unit Test Project (.NET Core) and call it <em>CarsServiceMvc.Tests</em>.</p>
<p>We would like to use <span style="color: #ff9900;">DefaultConnection</span> connection string defined in the main project. For that purpose, we right click on the <em>Tests</em> project, select <em>Add -&gt; Existing item &#8230;,</em> go back to the main project&#8217;s folder and select <em>appsettings.json</em> file, adding it as a link:</p>
<figure id="attachment_2335" aria-describedby="caption-attachment-2335" style="width: 349px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?ssl=1" target="_blank" rel="noopener"><img data-recalc-dims="1" decoding="async" data-attachment-id="2335" data-permalink="https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/addfileaslink/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?fit=699%2C471&amp;ssl=1" data-orig-size="699,471" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="AddFileAsLink" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?fit=699%2C471&amp;ssl=1" class="wp-image-2335 " src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?resize=349%2C235&#038;ssl=1" alt="" width="349" height="235" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?resize=300%2C202&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/AddFileAsLink.png?w=699&amp;ssl=1 699w" sizes="(max-width: 349px) 100vw, 349px" /></a><figcaption id="caption-attachment-2335" class="wp-caption-text">Adding config file as a link</figcaption></figure>
<p>This file should also be copied to the output directory of <em>Tests</em> project (it can be set in the Properties of the linked file).</p>
<p>Now we can add a new <em>TestClass</em> called <span style="color: #ff9900;">CarServiceContextInitTests</span> which will contain our db initialization Unit Test. In order to be able to access the same database as in the main project, we need to create few properties and configure necessary settings in the constructor of the test class:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72556847" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-carservicecontextinittests_ctor-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="CarServiceContextInitTests_ctor.cs content, created by dsibinski on 06:28PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="CarServiceContextInitTests_ctor.cs">
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC1" class="blob-code blob-code-inner js-file-line">  [TestClass]</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC2" class="blob-code blob-code-inner js-file-line">  public class CarServiceContextInitTests</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC3" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC4" class="blob-code blob-code-inner js-file-line">      // to have the same Configuration object as in Startup</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC5" class="blob-code blob-code-inner js-file-line">      private IConfigurationRoot _configuration;</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC6" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC7" class="blob-code blob-code-inner js-file-line">      // represents database&#39;s configuration</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC8" class="blob-code blob-code-inner js-file-line">      private DbContextOptions&lt;CarServiceContext&gt; _options;</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC9" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC10" class="blob-code blob-code-inner js-file-line">      public CarServiceContextInitTests()</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC11" class="blob-code blob-code-inner js-file-line">      {</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC12" class="blob-code blob-code-inner js-file-line">          var builder = new ConfigurationBuilder()</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC13" class="blob-code blob-code-inner js-file-line">              .SetBasePath(Directory.GetCurrentDirectory())</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC14" class="blob-code blob-code-inner js-file-line">              .AddJsonFile(&quot;appsettings.json&quot;);</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC15" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC16" class="blob-code blob-code-inner js-file-line">          _configuration = builder.Build();</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC17" class="blob-code blob-code-inner js-file-line">          _options = new DbContextOptionsBuilder&lt;CarServiceContext&gt;()</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC18" class="blob-code blob-code-inner js-file-line">              .UseSqlServer(_configuration.GetConnectionString(&quot;DefaultConnection&quot;))</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC19" class="blob-code blob-code-inner js-file-line">              .Options;</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC20" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-carservicecontextinittests_ctor-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-carservicecontextinittests_ctor-cs-LC21" class="blob-code blob-code-inner js-file-line">   }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/f8cbd51dbfb33ef360e16d1d56e164e1/raw/cda90ac2bbb252cb4650f89790472a56f171ee9c/CarServiceContextInitTests_ctor.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/f8cbd51dbfb33ef360e16d1d56e164e1#file-carservicecontextinittests_ctor-cs" class="Link--inTextBlock"><br />
          CarServiceContextInitTests_ctor.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As the <em>appsettings.json</em> file is copied to the <em>Tests</em> project&#8217;s output directory, we can read its contents with <span style="color: #ff9900;">ConfigurationBuilder</span> class. After, we just initialize <span style="color: #ff9900;">_options</span> property with the db connection settings.</p>
<p>Finally we can add the desired Unit Test method:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist72557144" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-initializedatabasewithdatatest_method-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="InitializeDatabaseWithDataTest_method.cs content, created by dsibinski on 06:32PM on July 27, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="InitializeDatabaseWithDataTest_method.cs">
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC1" class="blob-code blob-code-inner js-file-line">  [TestMethod]</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC2" class="blob-code blob-code-inner js-file-line">        public void InitializeDatabaseWithDataTest()</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC3" class="blob-code blob-code-inner js-file-line">        {</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC4" class="blob-code blob-code-inner js-file-line">            using (var context = new CarServiceContext(_options))</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC5" class="blob-code blob-code-inner js-file-line">            {</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC6" class="blob-code blob-code-inner js-file-line">                context.Database.EnsureDeleted();</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC7" class="blob-code blob-code-inner js-file-line">                context.Database.EnsureCreated();</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC9" class="blob-code blob-code-inner js-file-line">                var car1 = new Car()</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC10" class="blob-code blob-code-inner js-file-line">                {</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC11" class="blob-code blob-code-inner js-file-line">                    Producer = &quot;Volkswagen&quot;,</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC12" class="blob-code blob-code-inner js-file-line">                    Model = &quot;Golf IV&quot;,</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC13" class="blob-code blob-code-inner js-file-line">                    ProductionDate = new DateTime(2009, 01, 01),</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC14" class="blob-code blob-code-inner js-file-line">                    IsOnWarranty = false</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC15" class="blob-code blob-code-inner js-file-line">                };</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC16" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC17" class="blob-code blob-code-inner js-file-line">                var car2 = new Car()</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC18" class="blob-code blob-code-inner js-file-line">                {</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC19" class="blob-code blob-code-inner js-file-line">                    Producer = &quot;Peugeot&quot;,</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC20" class="blob-code blob-code-inner js-file-line">                    Model = &quot;206&quot;,</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC21" class="blob-code blob-code-inner js-file-line">                    ProductionDate = new DateTime(2000, 01, 01),</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC22" class="blob-code blob-code-inner js-file-line">                    IsOnWarranty = false</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC23" class="blob-code blob-code-inner js-file-line">                };</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC24" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC25" class="blob-code blob-code-inner js-file-line">                context.Cars.AddRange(car1, car2);</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC26" class="blob-code blob-code-inner js-file-line">                context.SaveChanges();</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC27" class="blob-code blob-code-inner js-file-line">            }</td>
</tr>
<tr>
<td id="file-initializedatabasewithdatatest_method-cs-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
<td id="file-initializedatabasewithdatatest_method-cs-LC28" class="blob-code blob-code-inner js-file-line">        }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/933f0a51f858026ce1643686e44996d5/raw/fde019a96e43fedb4e39344424376fc023088984/InitializeDatabaseWithDataTest_method.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/933f0a51f858026ce1643686e44996d5#file-initializedatabasewithdatatest_method-cs" class="Link--inTextBlock"><br />
          InitializeDatabaseWithDataTest_method.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>There are few important parts of this code:</p>
<p>Line 4: we used <span style="color: #ff9900;">_options</span> property to configure the <span style="color: #ff9900;">CarServiceContext <span style="color: #000000;">thanks to its second constructor<br />
</span></span></p>
<p>Lines 6 and 7: <span style="color: #ff9900;">EnsureDeleted()</span> method is called before <span style="color: #ff9900;">EnsureCreated()</span>, which makes the db being re-created every time Unit Test is run.</p>
<p>After, there&#8217;s just creation of two <span style="color: #ff9900;">Car</span> objects and inserting these into the database.</p>
<p>After this Unit Test is run, <em>Cars</em> table is created in the local database with two sample rows. When we launch the application there&#8217;s no more error:</p>
<figure id="attachment_2337" aria-describedby="caption-attachment-2337" style="width: 574px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?ssl=1" target="_blank" rel="noopener"><img data-recalc-dims="1" decoding="async" data-attachment-id="2337" data-permalink="https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/carslistmvc/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?fit=1000%2C535&amp;ssl=1" data-orig-size="1000,535" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="CarsListMvc" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?fit=1000%2C535&amp;ssl=1" class="wp-image-2337" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?resize=574%2C308&#038;ssl=1" alt="" width="574" height="308" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?resize=300%2C161&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?resize=768%2C411&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?resize=720%2C385&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/07/CarsListMvc.png?w=1000&amp;ssl=1 1000w" sizes="(max-width: 574px) 100vw, 574px" /></a><figcaption id="caption-attachment-2337" class="wp-caption-text">Cars list</figcaption></figure>
<p>From now on, every developer who starts working on the project only executes our unit test and his/her local database is created and filled with sample data.</p>
<p>Moreover, when there&#8217;s any change in the model class, there&#8217;s just one Unit Test to modify (e.g. if <strong>Required</strong> column is added to the model) and the other programmers just re-execute the Unit Test and database is up-to-date and ready to work with.</p>
<h1>Database initialization on production</h1>
<p>The Unit Test approach of database initialization could also be used on production environment, for instance when the application is to be installed for the first time on customer&#8217;s environment. We can create another Unit Test which creates the database structures, but doesn&#8217;t fill it with any data in a separate project. Then such Unit Tests project can be compiled to an executable and launched on production environment.</p>
<p>I know people who used it on production, but I don&#8217;t think I&#8217; be brave enough to go that far <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h1>Summary</h1>
<p>The database initialization with Unit Test is kind of a new approach I&#8217;ve never been thinking about. In my opinion it could be used with success during development phase, when the data in the database is not a production one. As soon as customer starts creating some business data in the application, it would probably make less sense to drop and create brand new database each time a developer starts working on the project (most of the bugs/issues we solve on production are dependent from the business data in the application).</p>
<p><span style="text-decoration: underline;"><strong>Did you know this approach of creating the database with Unit Test? What are the techniques you use to initialize the database when working with other developers? Let me know, I&#8217;m deeply interested how it looks on your daily basis <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></span></p>
<p>The post <a href="https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/">Entity Framework Core &#8211; database initialization with Unit Test</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/entity-framework-core-database-initialization-with-unit-test/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2331</post-id>	</item>
	</channel>
</rss>
