{"id":1995,"date":"2018-08-04T10:44:11","date_gmt":"2018-08-04T10:44:11","guid":{"rendered":"http:\/\/obioberoi.azurewebsites.net\/?p=1995"},"modified":"2019-02-19T11:34:13","modified_gmt":"2019-02-19T11:34:13","slug":"data-seeding-in-ef-core","status":"publish","type":"post","link":"https:\/\/obioberoi.com\/?p=1995","title":{"rendered":"Data Seeding in EF Core"},"content":{"rendered":"<p>Data seeding is a process where you populate the initial set of data into the database. I&#8217;ll explain some of the rudimentary steps needed for creating seed data in your app.<\/p>\n<p>Prior to EF Core 2.1, seeding data was not quite straight-forward. However with this version, you can seed data using the following steps:<\/p>\n<ol>\n<li>Download the necessary packages from NuGet<\/li>\n<li>Create your Context Class<\/li>\n<li>Write Seed Data<\/li>\n<li>Run Migrations<\/li>\n<li>Apply data changes to your database<\/li>\n<\/ol>\n<p>Let&#8217;s begin&#8230;<\/p>\n<p><strong>Download Packages:<\/strong> You need the following packages after you create a new .NET Core console app:<\/p>\n<ul>\n<li>Microsoft.EntityFrameworkCore<\/li>\n<li>Microsoft.EntityFrameworkCore.SqlServer<\/li>\n<li>Microsoft.EntityFrameworkCore.Tools<\/li>\n<\/ul>\n<p><strong>Context Class:<\/strong> Create a new context class which would derive from <em>DbContext<\/em>. <em>DbContext<\/em> represents a session with the database and is used to perform CRUD operations against your database. It&#8217;s also a combination of Unit of Work and Repository patterns.<\/p>\n<pre><code>\r\npublic class DataSeedContext : DbContext\r\n    {\r\n        public DbSet Blog { get; set; }\r\n\r\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder\r\n                .UseSqlServer(\r\n                @\"server=obi-oberoi; database=demo.DotNetConf; trusted_connection=true;\");\r\n        }\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            modelBuilder.Entity()\r\n                .Property(u =&gt; u.Url)\r\n                .IsRequired();\r\n\r\n            modelBuilder.Entity().HasData(\r\n                new Blog { BlogId = 1, Url = \"http:\/\/ObiOberoi.com\" },\r\n                new Blog { BlogId = 2, Url = \"http:\/\/microsoft.com\" },\r\n                new Blog { BlogId = 3, Url = \"http:\/\/cnn.com\" },\r\n                new Blog {  BlogId = 4, Url = \"https:\/\/yahoo.com\"},\r\n                new Blog { BlogId = 5, Url = \"https:\/\/tesla.com\" },\r\n                new Blog { BlogId = 6, Url = \"https:\/\/meetup.com\/mississauganetug\"},\r\n                new Blog { BlogId = 7, Url=\"https:\/\/td.com\"},\r\n                new Blog { BlogId = 8, Url=\"https:\/\/meetup.com\/TorontoNETUG\"}\r\n                );\r\n        }\r\n    }\r\n<\/code><\/pre>\n<p>Create a Blog class like so:<\/p>\n<pre><code>\r\npublic class Blog\r\n    {\r\n        public int BlogId { get; set; }\r\n        [Required]\r\n        public string Url { get; set; }\r\n    }\r\n<\/code>\r\n<\/pre>\n<p>Be sure to specify the type of database you wish to use. In the example, SQL Server is being used. This is defined in the Context class&#8217;s <em>OnConfiguring <\/em>method.<\/p>\n<pre><code>\r\nprotected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder\r\n                .UseSqlServer(\r\n                @\"server=obi-oberoi; database=demo.DotNetConf; trusted_connection=true;\");\r\n        }\r\n<\/code><\/pre>\n<p><strong>Run Migration:<\/strong> Just a quick overview of Migrations. Migrations allow you to keep your database in sync with your code. EF Core uses your mapping configurations to produce a snapshot of what your database should look like. Your model may represent tables, its primary keys, foreign keys, indexes, constraints and such.<\/p>\n<p>Whenever you make a change to your POCO class i.e. add or remove a property, you ought to run a PowerShell command like so:<\/p>\n<p><em>Add-Migration &lt;migration name&gt;<\/em><\/p>\n<p>The Add-Migration command computes a database schema from your entity classes and scaffolds a migration with <em>Up<\/em> and <em>Down<\/em> methods which consists of the current version of the database to the new version it computes.<\/p>\n<p><strong>Apply Changes to Database:<\/strong> You do this by simply issuing the following command in PowerShell and you are set:<\/p>\n<p><em>Update-Database<\/em><\/p>\n<p>So, this was a quick intro to seeding data into the database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data seeding is a process where you populate the initial set of data into the database. I&#8217;ll explain some of the rudimentary steps needed for creating seed data in your app. Prior to EF Core 2.1, seeding data was not &hellip; <a href=\"https:\/\/obioberoi.com\/?p=1995\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,7],"tags":[],"class_list":["post-1995","post","type-post","status-publish","format-standard","hentry","category-net","category-orm"],"_links":{"self":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts\/1995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1995"}],"version-history":[{"count":5,"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts\/1995\/revisions"}],"predecessor-version":[{"id":2023,"href":"https:\/\/obioberoi.com\/index.php?rest_route=\/wp\/v2\/posts\/1995\/revisions\/2023"}],"wp:attachment":[{"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obioberoi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}