{"id":68,"date":"2021-10-14T12:10:46","date_gmt":"2021-10-14T12:10:46","guid":{"rendered":"https:\/\/code.handwerkit.com\/?p=68"},"modified":"2021-10-14T12:20:36","modified_gmt":"2021-10-14T12:20:36","slug":"c-entity-speichern-oder-update-von-objekten","status":"publish","type":"post","link":"https:\/\/code.handwerkit.com\/?p=68","title":{"rendered":"C# Entity Speichern oder Update von Objekten"},"content":{"rendered":"\n<p>Einfaches Abspeichern von Objekten, oder ein Update wenn vorhanden:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-csharp\" data-file=\"saveUpdateEntity\" data-lang=\"C#\" data-show-lang=\"1\"><code>class Car\n    {\n        public int id { get; set; }\n\n        public string name { get; set; }\n\n        public double weight { get; set; }\n\n        public DateTime carEntryDate { get; set; }\n    }\n\n\n    class ClassCar\n    {\n        private async Task SaveUpdateCar(Car car = null)\n        {\n            try\n            {\n                \/\/open connection\n                using (var db = new dbEntities())\n                {\n                    \/\/new object of data \n                    var thCar = new Car();\n\n                    \/\/on update fill the object with the given data \n                    \/\/ie the unique id\n                    if (car != null)\n                    {\n                        thCar = db.zahlungen.Find(car.id);\n                    }\n                    \n                    \/\/set the properties to object if ther is a new object\n                    else\n                    {\n                        thCar.carEntryDate = DateTime.Now;\n                    }\n                    \n                    \/\/fill the object with new data\n                    car.name = &quot;Audi SQ7&quot;;\n                    car.weight = 1980;\n\n                        \n                        \/\/if add a new object\n                    if (car == null)\n                    {\n                        db.kalkulation_zahlungen.Add(thCar);\n                    }\n                    \/\/save the data to db oder save async\n                    db.SaveChangesAsync();                   \n                }\n            }\n            catch (Exception ex)\n            {\n                var fail = ex.Message;\n            }\n        }\n    }\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Einfaches Abspeichern von Objekten, oder ein Update wenn vorhanden:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14,1],"tags":[16,17,15],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-c-net","category-datenbanken","category-uncategorized","tag-database","tag-ef","tag-entity","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Rooout","author_link":"https:\/\/code.handwerkit.com\/?author=1"},"uagb_comment_info":1,"uagb_excerpt":"Einfaches Abspeichern von Objekten, oder ein Update wenn vorhanden:","_links":{"self":[{"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":3,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.handwerkit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}