{"id":512,"date":"2019-03-08T09:51:58","date_gmt":"2019-03-08T13:51:58","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=512"},"modified":"2019-03-08T10:06:18","modified_gmt":"2019-03-08T14:06:18","slug":"add-new-entity-or-copy-entity-with-entity-framework","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/add-new-entity-or-copy-entity-with-entity-framework\/","title":{"rendered":"Add New Entity (or Copy Entity) with Entity Framework"},"content":{"rendered":"<p>To add a new entity to your model, simply add it using <code>&lt;context&gt;.&lt;models&gt;.Add(newEntity)<\/code> and then call <code>&lt;context&gt;.SaveChanges()<\/code>.<\/p>\n<p>If you want to copy an existing entity and insert the copied item into the model, calling <code>&lt;context&gt;.&lt;models&gt;.Add(copiedEntity)<\/code> will add the entity to the model as a new entity, even if the Id is populated with an existing value.<\/p>\n<pre><code>using(MyDbContext context = new MyDbContext()) {\n    MyModel myModel = context.FirstOrDefault(m =&gt; m.SomeProperty == someValue);\n    myModel.SomeOtherProperty = someOtherValue; \/\/user changed a value\n    context.MyModels.Add(myModel); \/\/add entity &amp; autoincrement Id        \n    database.SaveChanges();\n}\n<\/code><\/pre>\n<p>Source: <a href=\"https:\/\/stackoverflow.com\/a\/25722197\/4669143\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/stackoverflow.com\/a\/25722197\/4669143<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To add a new entity to your model, simply add it using &lt;context&gt;.&lt;models&gt;.Add(newEntity) and then call &lt;context&gt;.SaveChanges(). If you want to copy an existing entity and insert the copied item  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-entity-framework"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-8g","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":2,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/512\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}