{"id":853,"date":"2010-04-30T10:07:00","date_gmt":"2010-04-30T10:07:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/cesardelatorre\/2010\/04\/30\/using-entity-framework-self-tracking-entities-with-appfabric-cache-velocity-and-net-4-0-rtm\/"},"modified":"2010-04-30T10:07:00","modified_gmt":"2010-04-30T10:07:00","slug":"using-entity-framework-self-tracking-entities-with-appfabric-cache-velocity-and-net-4-0-rtm","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/using-entity-framework-self-tracking-entities-with-appfabric-cache-velocity-and-net-4-0-rtm\/","title":{"rendered":"Using Entity Framework Self Tracking Entities with AppFabric CACHE (\u2018Velocity\u2019) and .NET 4.0 RTM"},"content":{"rendered":"<p><P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_2.png\"><IMG title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_thumb.png\" width=\"231\" height=\"72\"><\/A> <A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_4.png\"><IMG title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_thumb_1.png\" width=\"238\" height=\"70\"><\/A> <\/P>\n<P>Currently, when you try to use <STRONG><EM>Entity Framework STE (Self Tracking Entities)<\/EM><\/STRONG> and <STRONG><EM>AppFabric-Cache<\/EM><\/STRONG> (aka. \u201c<STRONG>Velocity<\/STRONG>\u201d), you\u2019ll get a runtime error. <\/P>\n<P>I mean, what we\u2019re trying to do is trying to serialize STE objects within AppFabric-Cache. For instance, we could try to execute the following code, using AppFabric Cache API:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"2\" width=\"733\">\n<TBODY>\n<TR>\n<TD vAlign=\"top\" width=\"731\">\n<P><FONT size=\"3\" face=\"Courier New\">var cacheContainer = this._cacheFactory.GetCache(container); <BR>cacheContainer.<STRONG>Put<\/STRONG>(key, value, timeout, region);<\/FONT><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Where, in this case, the \u2018value\u2019 parameter would be an EF STE object or collection, ok?<\/P>\n<P>Then, when trying to execute that Put() command to store it within AppFabric, you\u2019ll get the following \u201cnice\u201d error:<\/P>\n<P><STRONG><EM>System.Runtime.Serialization.InvalidDataContractException was unhandled by user code<\/EM><\/STRONG><\/P>\n<P><STRONG><EM>Message=Type &#8216;XXXXXXXXXXXXXXXXXX.Entities.TrackableCollection`1[XXXXXXXXXXXXXXXXXXXXXXXXXXxx.MyEntity]&#8217; cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.<\/EM><\/STRONG><\/P>\n<P>The problem is that by default the <STRONG>TrackableCollection&lt;T&gt;<\/STRONG> in the <STRONG>Types.tt<\/STRONG> file (T4 template), is not marked with a <STRONG>[CollectionDataContract]<\/STRONG> attribute. <\/P>\n<P>You can fix this by going to Line 1401 in the in the <STRONG>Types.tt<\/STRONG> (usually it is that line number) and adding the [CollectionDataContract] attribute like the following:<\/P>\n<P><FONT size=\"3\"><FONT face=\"Courier New\"><STRONG>[CollectionDataContract]<\/STRONG> <BR>public class TrackableCollection&lt;T&gt; : ObservableCollection&lt;T&gt;<\/FONT><\/FONT><\/P>\n<P>Then, you\u2019ll be able to serialize and store your Entity Framework Self Tracking Entities within AppFabric CACHE. \ud83d\ude42 <\/P>\n<P>BTW, I think that the same problem could arise if you try to use <STRONG>Workflow Foundation Persistence<\/STRONG> and <STRONG><EM>Entity Framework STE (Self Tracking Entities). <\/EM><\/STRONG>Its workaround could be the same.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently, when you try to use Entity Framework STE (Self Tracking Entities) and AppFabric-Cache (aka. \u201cVelocity\u201d), you\u2019ll get a runtime error. I mean, what we\u2019re trying to do is trying to serialize STE objects within AppFabric-Cache. For instance, we could try to execute the following code, using AppFabric Cache API: var cacheContainer = this._cacheFactory.GetCache(container); cacheContainer.Put(key, [&hellip;]<\/p>\n","protected":false},"author":362,"featured_media":12806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[5,18,48],"class_list":["post-853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cesardelatorre","tag-net-4-0","tag-appfabric","tag-entity-framework"],"acf":[],"blog_post_summary":"<p>Currently, when you try to use Entity Framework STE (Self Tracking Entities) and AppFabric-Cache (aka. \u201cVelocity\u201d), you\u2019ll get a runtime error. I mean, what we\u2019re trying to do is trying to serialize STE objects within AppFabric-Cache. For instance, we could try to execute the following code, using AppFabric Cache API: var cacheContainer = this._cacheFactory.GetCache(container); cacheContainer.Put(key, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media\/12806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}