{"id":4154,"date":"2016-07-24T20:45:54","date_gmt":"2016-07-24T20:45:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4154"},"modified":"2016-07-24T20:45:54","modified_gmt":"2016-07-24T20:45:54","slug":"returning-to-entity-framework-database-first","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/returning-to-entity-framework-database-first\/","title":{"rendered":"Returning to Entity Framework database first"},"content":{"rendered":"<p>After working with a database project in Visual Studio, I thought it was probably time to create a simple console application to interact with the database using the current version of Entity Framework (v6.0).<\/p>\n<p>So as we&#8217;ve already created the cddb database in a previous post, we&#8217;ll simply create a new console project and work with that DB.<\/p>\n<ul>\n<li>Create your application, as stated mine is a console application<\/li>\n<li>Add new item and select Data | ADO.NET Entity Data Model, mine&#8217;s named CddbContext (as this will include the source for the data context created for EF)<\/li>\n<li>Select Code First from database<\/li>\n<li>Create a new connection and supply the relevant details for your database connection<\/li>\n<li>Press next then select the tables (and views) you want to generate code for &#8211; then click Finish<\/li>\n<\/ul>\n<p>Here&#8217;s the code generated<\/p>\n<p>CddbContext.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic partial class CddbContext : DbContext\r\n{\r\n   public CddbContext()\r\n      : base(&quot;name=CddbContext&quot;)\r\n   {\r\n   }\r\n\r\n   public virtual DbSet&lt;album&gt; albums { get; set; }\r\n   public virtual DbSet&lt;artist&gt; artists { get; set; }\r\n\r\n   protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n   {\r\n      modelBuilder.Entity&lt;artist&gt;()\r\n         .HasMany(e =&gt; e.albums)\r\n         .WithRequired(e =&gt; e.artist)\r\n         .WillCascadeOnDelete(false);\r\n   }\r\n}\r\n<\/pre>\n<p>artist.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Table(&quot;artist&quot;)]\r\npublic partial class artist\r\n{\r\n   &#x5B;System.Diagnostics.CodeAnalysis.SuppressMessage(\r\n    &quot;Microsoft.Usage&quot;, \r\n    &quot;CA2214:DoNotCallOverridableMethodsInConstructors&quot;)]\r\n   public artist()\r\n   {\r\n      albums = new HashSet&lt;album&gt;();\r\n   }\r\n\r\n   public int Id { get; set; }\r\n\r\n   &#x5B;Required]\r\n   &#x5B;StringLength(50)]\r\n   public string Name { get; set; }\r\n\r\n   &#x5B;System.Diagnostics.CodeAnalysis.SuppressMessage(\r\n    &quot;Microsoft.Usage&quot;, \r\n    &quot;CA2227:CollectionPropertiesShouldBeReadOnly&quot;)]\r\n   public virtual ICollection&lt;album&gt; albums { get; set; }\r\n}\r\n<\/pre>\n<p>album.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Table(&quot;album&quot;)]\r\npublic partial class album\r\n{\r\n   public int Id { get; set; }\r\n\r\n   &#x5B;Required]\r\n   &#x5B;StringLength(50)]\r\n   public string Title { get; set; }\r\n\r\n   public int ArtistId { get; set; }\r\n\r\n   public virtual artist artist { get; set; }\r\n}\r\n<\/pre>\n<p>finally let&#8217;s create a simple but of code to get the artists from the database, so in Main we have<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var db = new CddbContext())\r\n{\r\n   var artists = db.artists;\r\n   foreach (var a in artists)\r\n   {\r\n      Console.WriteLine(a.Name);\r\n   }\r\n}\r\n<\/pre>\n<p>If your database schema changes you will need to re-run the steps to generate your data context etc. or code by hand. There isn&#8217;t (currently) a way to update existing classes &#8211; so don&#8217;t make changes to the generated code and expect it to still exist after regeneration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After working with a database project in Visual Studio, I thought it was probably time to create a simple console application to interact with the database using the current version of Entity Framework (v6.0). So as we&#8217;ve already created the cddb database in a previous post, we&#8217;ll simply create a new console project and work [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22,21],"tags":[],"class_list":["post-4154","post","type-post","status-publish","format-standard","hentry","category-database","category-entity-framework"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4154"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4154\/revisions"}],"predecessor-version":[{"id":4180,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4154\/revisions\/4180"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}