Why is my Entity Framework Code First proxy collection null and why can’t I set it?

As you correctly observed in the answer to your own question, removing the “virtual” keyword from the collection properties works around the problem, by preventing the Entity Framework from creating a change tracking proxy. However, this is not a solution for many people, because change tracking proxies can be really convenient and can help prevent … Read more

Entity Framework 4: Does it make sense to create a single diagram for all entities?

Having one big EDM containing all the entities generally is NOT a good practice and is not recommended. Using one large EDM will cause several issues such as: Performance Issue in Metadata Load Times: As the size of the schema files increase, the time it takes to parse and create an in-memory model for this … Read more

Is it possible to prevent EntityFramework 4 from overwriting customized properties?

A different approach is to hook into the ObjectMaterialized event in the DbContext and set the kind there. In my DbContext constructor, i do this: ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += new ObjectMaterializedEventHandler(ObjectMaterialized); and then the method looks like this: private void ObjectMaterialized(object sender, ObjectMaterializedEventArgs e) { Person person = e.Entity as Person; if (person != null) // the … Read more

ASP.NET MVC / EF4 / POCO / Repository – How to Update Relationships?

I’ve accepted @jfar’s answer because he put me on the right track, but thought i’d add an answer here for other people’s benefit. The reason the relationships were not getting updated is for the following reasons: 1) Completely disconnected scenario. ASP.NET = stateless, new context newed up each HTTP request. 2) Edited entity created by … Read more

How to serialize/deserialize simple classes to XML and back

XmlSerializer is one way to do it. DataContractSerializer is another. Example with XmlSerializer: using System.Xml; using System.Xml.Serialization; //… ShoppingCart shoppingCart = FetchShoppingCartFromSomewhere(); var serializer = new XmlSerializer(shoppingCart.GetType()); using (var writer = XmlWriter.Create(“shoppingcart.xml”)) { serializer.Serialize(writer, shoppingCart); } and to deserialize it back: var serializer = new XmlSerializer(typeof(ShoppingCart)); using (var reader = XmlReader.Create(“shoppingcart.xml”)) { var shoppingCart = … Read more