{"id":1028,"date":"2010-04-20T22:45:18","date_gmt":"2010-04-20T21:45:18","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=1028"},"modified":"2010-04-20T22:45:18","modified_gmt":"2010-04-20T21:45:18","slug":"entity-framework-4-tobbretegu-appokban","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2010\/04\/20\/entity-framework-4-tobbretegu-appokban\/","title":{"rendered":"Entity Framework 4 t\u00f6bbr\u00e9teg\u0171 appokban"},"content":{"rendered":"<p>J\u00e1tszottam kicsit az EF4-gyel. Az al\u00e1bbi k\u00f3d egy n r\u00e9teg\u0171 app adatmozg\u00e1s\u00e1t szimul\u00e1lja a WCF xml szerializ\u00e1l\u00f3j\u00e1t haszn\u00e1lva. Mindh\u00e1rom template-tel kipr\u00f3b\u00e1ltam, al\u00e1bb l\u00e1that\u00f3ak az adatmozg\u00e1sok.<br \/>\nA tesztk\u00f3d messze nem korrekt, de kiindul\u00f3pontk\u00e9nt tov\u00e1bbi vizsg\u00e1latokhoz elfogathat\u00f3:<\/p>\n<p>using System;<br \/>\nusing System.IO;<br \/>\nusing System.Linq;<br \/>\nusing System.Runtime.Serialization;<\/p>\n<p>namespace POCO1<br \/>\n{<br \/>\n    class Program<br \/>\n    {<br \/>\n        static void Main()<br \/>\n        {<br \/>\n            Department d;<br \/>\n            using (var e = new SchoolEntities())<br \/>\n            {<br \/>\n                e.ContextOptions.ProxyCreationEnabled = false;<br \/>\n                e.ContextOptions.LazyLoadingEnabled = false;<br \/>\n                d = e.Departments.Include(&#8220;Courses&#8221;).Single(dep => dep.DepartmentID == 1);<br \/>\n                Console.WriteLine(&#8220;{0}&#8221;, d.Name);<br \/>\n                Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8220;);<br \/>\n                foreach (Course c in d.Courses)<br \/>\n                {<br \/>\n                    Console.WriteLine(&#8220;{0}&#8221;, c.Title);<br \/>\n                }<br \/>\n                e.Detach(d);<br \/>\n            }<\/p>\n<p>            var ser = new DataContractSerializer(d.GetType());<br \/>\n            \/\/var ser = new DataContractSerializer(d.GetType(),<br \/>\n            \/\/null, 50000, true, true, null, new ProxyDataContractResolver());<\/p>\n<p>            using (var s2c = new FileStream(@&#8221;c:\\temp\\Server2Client.xml&#8221;, FileMode.Create, FileAccess.ReadWrite))<br \/>\n            {<br \/>\n                \/\/1. Server k\u00fcld kliensre<br \/>\n                ser.WriteObject(s2c, d);<br \/>\n                s2c.Position = 0;<br \/>\n                \/\/2. Kliens deserializ\u00e1l<br \/>\n                var clientSideDep = (Department)ser.ReadObject(s2c);<\/p>\n<p>                \/\/Csak ST<br \/>\n                \/\/bool ce = clientSideDep.ChangeTracker.ChangeTrackingEnabled;<\/p>\n<p>                \/\/3. Kliens m\u00f3dos\u00edt<br \/>\n                clientSideDep.Name += &#8220;a&#8221;;<\/p>\n<p>                using (var c2s = new FileStream(@&#8221;c:\\temp\\Client2Server.xml&#8221;, FileMode.Create, FileAccess.ReadWrite))<br \/>\n                {<br \/>\n                    \/\/4. Kliens visszak\u00fcld<br \/>\n                    ser.WriteObject(c2s, clientSideDep);<br \/>\n                    c2s.Position = 0;<\/p>\n<p>                    \/\/5.Server deserializ\u00e1l<br \/>\n                    var sentBackDepartment = (Department)ser.ReadObject(c2s);<br \/>\n                    using (var e = new SchoolEntities())<br \/>\n                    {<br \/>\n                        \/\/6. Server visszam\u00f3dos\u00edt<\/p>\n<p>                        \/\/Norm\u00e1l entit\u00e1s<br \/>\n                        e.Departments.Attach(sentBackDepartment);<br \/>\n                        e.ObjectStateManager.GetObjectStateEntry(sentBackDepartment).SetModified();<br \/>\n                        e.ObjectStateManager.GetObjectStateEntry(sentBackDepartment).SetModifiedProperty(&#8220;Name&#8221;);<\/p>\n<p>                        \/\/POCO<br \/>\n                        \/\/e.Departments.Include(&#8220;Courses&#8221;).Single(dep => dep.DepartmentID == 1);<br \/>\n                        \/\/e.Departments.ApplyCurrentValues(sentBackDepartment);<\/p>\n<p>                        \/\/Self-tracking entity<br \/>\n                        \/\/e.Departments.ApplyChanges(sentBackDepartment);<\/p>\n<p>                        e.SaveChanges();<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>EF alapobjektumok szerviz => kliens:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Department z:Id=&quot;i1&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/POCO1&quot; xmlns:i=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:z=&quot;http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/&quot;&gt;\r\n  &lt;EntityKey z:Id=&quot;i2&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/System.Data.Objects.DataClasses&quot; xmlns:a=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/System.Data&quot;&gt;\r\n    &lt;a:EntityContainerName&gt;SchoolEntities&lt;\/a:EntityContainerName&gt;\r\n    &lt;a:EntityKeyValues&gt;\r\n      &lt;a:EntityKeyMember&gt;\r\n        &lt;a:Key&gt;DepartmentID&lt;\/a:Key&gt;\r\n        &lt;a:Value i:type=&quot;b:int&quot; xmlns:b=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;&gt;1&lt;\/a:Value&gt;\r\n      &lt;\/a:EntityKeyMember&gt;\r\n    &lt;\/a:EntityKeyValues&gt;\r\n    &lt;a:EntitySetName&gt;Departments&lt;\/a:EntitySetName&gt;\r\n  &lt;\/EntityKey&gt;\r\n  &lt;Administrator&gt;2&lt;\/Administrator&gt;\r\n  &lt;Budget&gt;350000.0000&lt;\/Budget&gt;\r\n  &lt;Courses\/&gt;\r\n  &lt;DepartmentID&gt;1&lt;\/DepartmentID&gt;\r\n  &lt;Name&gt;Engineering&lt;\/Name&gt;\r\n  &lt;StartDate&gt;2007-09-01T00:00:00&lt;\/StartDate&gt;\r\n&lt;\/Department&gt;\r\n<\/pre>\n<p>EF alapobjektumok kliens => szerviz:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Department z:Id=&quot;i1&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/POCO1&quot; xmlns:i=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:z=&quot;http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/&quot;&gt;\r\n  &lt;EntityKey z:Id=&quot;i2&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/System.Data.Objects.DataClasses&quot; xmlns:a=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/System.Data&quot;&gt;\r\n    &lt;a:EntityContainerName&gt;SchoolEntities&lt;\/a:EntityContainerName&gt;\r\n    &lt;a:EntityKeyValues&gt;\r\n      &lt;a:EntityKeyMember&gt;\r\n        &lt;a:Key&gt;DepartmentID&lt;\/a:Key&gt;\r\n        &lt;a:Value i:type=&quot;b:int&quot; xmlns:b=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;&gt;1&lt;\/a:Value&gt;\r\n      &lt;\/a:EntityKeyMember&gt;\r\n    &lt;\/a:EntityKeyValues&gt;\r\n    &lt;a:EntitySetName&gt;Departments&lt;\/a:EntitySetName&gt;\r\n  &lt;\/EntityKey&gt;\r\n  &lt;Administrator&gt;2&lt;\/Administrator&gt;\r\n  &lt;Budget&gt;350000.0000&lt;\/Budget&gt;\r\n  &lt;Courses\/&gt;\r\n  &lt;DepartmentID&gt;1&lt;\/DepartmentID&gt;\r\n  &lt;Name&gt;Engineeringa&lt;\/Name&gt;\r\n  &lt;StartDate&gt;2007-09-01T00:00:00&lt;\/StartDate&gt;\r\n&lt;\/Department&gt;\r\n<\/pre>\n<p>POCO szerviz => kliens:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Department xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/POCO1&quot; xmlns:i=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;&gt;\r\n  &lt;Administrator&gt;2&lt;\/Administrator&gt;\r\n  &lt;Budget&gt;350000.0000&lt;\/Budget&gt;\r\n  &lt;Courses\/&gt;\r\n  &lt;DepartmentID&gt;1&lt;\/DepartmentID&gt;\r\n  &lt;Name&gt;Engineeringa&lt;\/Name&gt;\r\n  &lt;StartDate&gt;2007-09-01T00:00:00&lt;\/StartDate&gt;\r\n&lt;\/Department&gt;\r\n<\/pre>\n<p>POCO kliens => szerviz:<\/p>\n<p><Department xmlns=\"http:\/\/schemas.datacontract.org\/2004\/07\/POCO1\" xmlns:i=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"><br \/>\n  <Administrator>2<\/Administrator><br \/>\n  <Budget>350000.0000<\/Budget><br \/>\n  <Courses\/><br \/>\n  <DepartmentID>1<\/DepartmentID><br \/>\n  <Name>Engineeringaa<\/Name><br \/>\n  <StartDate>2007-09-01T00:00:00<\/StartDate><br \/>\n<\/Department><\/p>\n<p>Self-tracking entity, szerviz => kliens:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Department z:Id=&quot;i1&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/POCO1&quot; xmlns:i=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:z=&quot;http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/&quot;&gt;\r\n  &lt;Administrator&gt;2&lt;\/Administrator&gt;\r\n  &lt;Budget&gt;350000.0000&lt;\/Budget&gt;\r\n  &lt;ChangeTracker z:Id=&quot;i2&quot;&gt;\r\n    &lt;ExtendedProperties\/&gt;\r\n    &lt;ObjectsAddedToCollectionProperties\/&gt;\r\n    &lt;ObjectsRemovedFromCollectionProperties\/&gt;\r\n    &lt;OriginalValues\/&gt;\r\n    &lt;State&gt;Unchanged&lt;\/State&gt;\r\n  &lt;\/ChangeTracker&gt;\r\n  &lt;Courses\/&gt;\r\n  &lt;DepartmentID&gt;1&lt;\/DepartmentID&gt;\r\n  &lt;Name&gt;Engineeringaaaaaaaa&lt;\/Name&gt;\r\n  &lt;StartDate&gt;2007-09-01T00:00:00&lt;\/StartDate&gt;\r\n&lt;\/Department&gt;\r\n<\/pre>\n<p>Self-tracking entity, kliens => szerviz:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Department z:Id=&quot;i1&quot; xmlns=&quot;http:\/\/schemas.datacontract.org\/2004\/07\/POCO1&quot; xmlns:i=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:z=&quot;http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/&quot;&gt;\r\n  &lt;Administrator&gt;2&lt;\/Administrator&gt;\r\n  &lt;Budget&gt;350000.0000&lt;\/Budget&gt;\r\n  &lt;ChangeTracker z:Id=&quot;i2&quot;&gt;\r\n    &lt;ExtendedProperties\/&gt;\r\n    &lt;ObjectsAddedToCollectionProperties\/&gt;\r\n    &lt;ObjectsRemovedFromCollectionProperties\/&gt;\r\n    &lt;OriginalValues\/&gt;\r\n    &lt;State&gt;Modified&lt;\/State&gt;\r\n  &lt;\/ChangeTracker&gt;\r\n  &lt;Courses\/&gt;\r\n  &lt;DepartmentID&gt;1&lt;\/DepartmentID&gt;\r\n  &lt;Name&gt;Engineeringaaaaaaaaa&lt;\/Name&gt;\r\n  &lt;StartDate&gt;2007-09-01T00:00:00&lt;\/StartDate&gt;\r\n&lt;\/Department&gt;\r\n<\/pre>\n<p>\u00c9rdekes, hogy a m\u00f3dos\u00edt\u00e1s t\u00e9nye csak a ST-ben l\u00e1tszik, m\u00e9g az eredeti entity-sben sem. A POCO-t\u00f3l nem is v\u00e1rtuk persze.<br \/>\nK\u00e9s\u0151bb m\u00e9g majd foglalkozok b\u0151vebben a t\u00e9m\u00e1val. Aki j\u00e1tszani akar vele, hozza l\u00e9tre a School EF p\u00e9ldaadatb\u00e1zist, azon lehet futtatni.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>J\u00e1tszottam kicsit az EF4-gyel. Az al\u00e1bbi k\u00f3d egy n r\u00e9teg\u0171 app adatmozg\u00e1s\u00e1t szimul\u00e1lja a WCF xml szerializ\u00e1l\u00f3j\u00e1t haszn\u00e1lva. Mindh\u00e1rom template-tel kipr\u00f3b\u00e1ltam, al\u00e1bb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,75,25,6,74,4],"tags":[],"class_list":["post-1028","post","type-post","status-publish","format-standard","hentry","category-net","category-net-4","category-adonet","category-adatbazisok","category-entity-framework","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1028"}],"version-history":[{"count":3,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"predecessor-version":[{"id":1031,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1028\/revisions\/1031"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}