We have some code that depends on the correct setting of the IsChanged flag on Primary Key fields. We create entities on the presentation layer and they are passed via remoting to the service layer for processing. We are seeing a behavioural difference between entities participating in an inheritance hierarchy and entities not participating in an inheritance hierarchy.
Scenario 1: create a “normal” entity (not participating in an inheritance hierarchy) and set some fields but DON’T TOUCH the (non-identity) PK field then pass it to the service layer (thus it is serialized and deserialized). On the service layer the PK of the entity has IsChanged==false
Scenario 2: create an entity which is a subclass of another entity, set some fields but DON’T TOUCH the (non-identity) PK field (inherited from base) nor the FK to the PK. Then pass it to the Service layer. On the service layer, the PK of the base entity has IsChanged==true
Scenario 2 is not expected and in fact is detrimental to some of our other code that depends on IsChanged being false (our own PK "next id" handling code).
This seems like a bug. Can you fix it or else tell me what’s happening?
Here is some sample code to demonstrate the two scenarios (derived from the code example in the documentation on serialization in compact format):
// serialize and deserialize an entity NOT participating in an inheritance hierarchy
CONSUMEREntity consumer = new CONSUMEREntity();
consumer.NAME1 = "test";
StreamWriter writer = new StreamWriter("c:\\testconsumer.xml");
string consumerXml = String.Empty;
consumer.WriteXml(
XmlFormatAspect.Compact | XmlFormatAspect.DatesInXmlDataType | XmlFormatAspect.MLTextInCDataBlocks, out consumerXml);
writer.Write(consumerXml);
writer.Close();
// now read it back
StreamReader reader = new StreamReader("c:\\testconsumer.xml");
string xmlFromFile = reader.ReadToEnd();
reader.Close();
CONSUMEREntity consumerFromXml = new CONSUMEREntity();
consumerFromXml.ReadXml(xmlFromFile);
// NOTE: consumer.ID.IsChanged == false and consumerFromXml.ID.IsChanged == false ALL AS EXPECTED
// serialize and deserialize an entity THAT IS participating in an inheritance hierarchy: DeliveryChannelEntity derives from ChannelEntity
DELIVERYCHANNELEntity deliverychannel = new DELIVERYCHANNELEntity();
deliverychannel.NAME = "test channel";
writer = new StreamWriter("c:\\test.xml");
string deliveryChannelXml = String.Empty;
deliverychannel.WriteXml(
XmlFormatAspect.Compact | XmlFormatAspect.DatesInXmlDataType | XmlFormatAspect.MLTextInCDataBlocks, out deliveryChannelXml);
writer.Write(deliveryChannelXml);
writer.Close();
// now read it back
reader = new StreamReader("c:\\test.xml");
xmlFromFile = reader.ReadToEnd();
reader.Close();
DELIVERYCHANNELEntity dcFromXml = new DELIVERYCHANNELEntity();
dcFromXml.ReadXml(xmlFromFile);
// NOTE: deliveryChannel.ID.IsChanged == false but dcFromXml.ID.IsChanged == true WHICH IS NOT EXPECTED!
In the serialized XML:
For consumer:
<Fields>
<ID>
<CurrentValue Type="Unknown" />
<DbValue Type="Unknown" />
<IsChanged>False</IsChanged>
<IsNull>False</IsNull>
</ID>
For DeliveryChannel:
<Fields>
<ID>
<CurrentValue Type="Unknown" />
<DbValue Type="Unknown" />
<IsChanged>False</IsChanged>
<IsNull>False</IsNull>
</ID>
So the serialized version is correct. The problem must be in the deserialization code.
Please help!
(using v2.0 - About: August 3rd, 2006, ORM versiono: 2.0.0.60717)