{"id":192,"date":"2014-02-13T21:25:28","date_gmt":"2014-02-13T21:25:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=192"},"modified":"2014-02-13T21:25:28","modified_gmt":"2014-02-13T21:25:28","slug":"entity-framework-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/entity-framework-tips-and-tricks\/","title":{"rendered":"Entity Framework &#8211; Tips and Tricks"},"content":{"rendered":"<p><strong>How do I stop my Code First implementation creating a new database<\/strong><\/p>\n<p>It might seem a strange request, why use Code First then. But let&#8217;s assume you&#8217;ve created a Code First EF model and then switched to creating the db using SQL scripts and want ensure the EF application doesn&#8217;t try to create the db.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDatabase.SetInitializer(null);\r\n<\/pre>\n<p>By default the initializer would be set to CreateDatabaseIfNotExists.<\/p>\n<p>See also <a title=\"Database.SetInitializer\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/gg679461(v=vs.103).aspx\" target=\"_blank\">Database.SetInitializer<\/a>.<\/p>\n<p><strong>There is already an open DataReader associated with this Command which must be closed first<\/strong><\/p>\n<p>An EntityCommandExecutionException with the message &#8220;There is already an open DataReader associated with this Command which must be closed first&#8221; occurred whilst implementing a bit of code to list all the ProductTypes in a database. <\/p>\n<p>The solution appears to be add<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nMultipleActiveResultSets=True\r\n<\/pre>\n<p>to your connection string<\/p>\n<p>I&#8217;ll add more to this post as or when I remember<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I stop my Code First implementation creating a new database It might seem a strange request, why use Code First then. But let&#8217;s assume you&#8217;ve created a Code First EF model and then switched to creating the db using SQL scripts and want ensure the EF application doesn&#8217;t try to create the db. [&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":[21,2],"tags":[],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-entity-framework","category-programming"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/192","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=192"}],"version-history":[{"count":13,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}