Dynamics 365 Entities Serialization Issue

I am working on an application that uses Dynamics 365 (CRM) as the data store. I have a ServiceStack api sitting between my custom application and Dynamics. When serializing data from Dynamics to my DTO, sometimes it works and other times I get empty objects back.

Lets say I have query that I expect to get two records back. When I set a break point, I can see that Dynamics is returning the two items as expected. When ServiceStack converts the objects to my DTO objects I get what I expected about 80% of the time, the rest of the time I get an empty DTO’s. Which result in a JSON object array with the correct number of items, but the items have null or empty strings for the values.

Has anyone else seen this sort of things happen.

Here is my CRM entity code. Which was generated by the Early Code Generator for CRM.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CrmDemo.CrmDomainModel.Entities
{

[System.Runtime.Serialization.DataContractAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "8.2.1.8676")]
public enum idoe_schoolState
{
	
	[System.Runtime.Serialization.EnumMemberAttribute()]
	Active = 0,
	
	[System.Runtime.Serialization.EnumMemberAttribute()]
	Inactive = 1,
}

/// <summary>
/// 
/// </summary>
[System.Runtime.Serialization.DataContractAttribute()]
[Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute("idoe_school")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "8.2.1.8676")]
public partial class idoe_school : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{
	
	/// <summary>
	/// Default Constructor.
	/// </summary>
	[System.Diagnostics.DebuggerNonUserCode()]
	public idoe_school() : 
			base(EntityLogicalName)
	{
	}
	
	public const string EntityLogicalName = "idoe_school";
	
	public const string PrimaryIdAttribute = "idoe_schoolid";
	
	public const string PrimaryNameAttribute = "idoe_name";
	
	public const int EntityTypeCode = 10006;
	
	public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
	
	public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
	
	[System.Diagnostics.DebuggerNonUserCode()]
	private void OnPropertyChanged(string propertyName)
	{
		if ((this.PropertyChanged != null))
		{
			this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
		}
	}
	
	[System.Diagnostics.DebuggerNonUserCode()]
	private void OnPropertyChanging(string propertyName)
	{
		if ((this.PropertyChanging != null))
		{
			this.PropertyChanging(this, new System.ComponentModel.PropertyChangingEventArgs(propertyName));
		}
	}
	
	/// <summary>
	/// Unique identifier of the user who created the record.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdby")]
	public Microsoft.Xrm.Sdk.EntityReference CreatedBy
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("createdby");
		}
	}
	
	/// <summary>
	/// Date and time when the record was created.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdon")]
	public System.Nullable<System.DateTime> CreatedOn
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<System.DateTime>>("createdon");
		}
	}
	
	/// <summary>
	/// Unique identifier of the delegate user who created the record.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdonbehalfby")]
	public Microsoft.Xrm.Sdk.EntityReference CreatedOnBehalfBy
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("createdonbehalfby");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("CreatedOnBehalfBy");
			this.SetAttributeValue("createdonbehalfby", value);
			this.OnPropertyChanged("CreatedOnBehalfBy");
		}
	}
	
	/// <summary>
	/// 
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_activestatus")]
	public Microsoft.Xrm.Sdk.EntityReference idoe_ActiveStatus
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("idoe_activestatus");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_ActiveStatus");
			this.SetAttributeValue("idoe_activestatus", value);
			this.OnPropertyChanged("idoe_ActiveStatus");
		}
	}
	
	/// <summary>
	/// Unique identifier for Corporation associated with School.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_corporationid")]
	public Microsoft.Xrm.Sdk.EntityReference idoe_CorporationId
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("idoe_corporationid");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_CorporationId");
			this.SetAttributeValue("idoe_corporationid", value);
			this.OnPropertyChanged("idoe_CorporationId");
		}
	}
	
	/// <summary>
	/// The name of the custom entity.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_name")]
	public string idoe_name
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<string>("idoe_name");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_name");
			this.SetAttributeValue("idoe_name", value);
			this.OnPropertyChanged("idoe_name");
		}
	}
	
	/// <summary>
	/// Unique identifier for entity instances
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_schoolid")]
	public System.Nullable<System.Guid> idoe_schoolId
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<System.Guid>>("idoe_schoolid");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_schoolId");
			this.SetAttributeValue("idoe_schoolid", value);
			if (value.HasValue)
			{
				base.Id = value.Value;
			}
			else
			{
				base.Id = System.Guid.Empty;
			}
			this.OnPropertyChanged("idoe_schoolId");
		}
	}
	
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_schoolid")]
	public override System.Guid Id
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return base.Id;
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.idoe_schoolId = value;
		}
	}
	
	/// <summary>
	/// 
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_schoolnumber")]
	public string idoe_SchoolNumber
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<string>("idoe_schoolnumber");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_SchoolNumber");
			this.SetAttributeValue("idoe_schoolnumber", value);
			this.OnPropertyChanged("idoe_SchoolNumber");
		}
	}
	
	/// <summary>
	/// Sequence number of the import that created this record.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("importsequencenumber")]
	public System.Nullable<int> ImportSequenceNumber
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<int>>("importsequencenumber");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("ImportSequenceNumber");
			this.SetAttributeValue("importsequencenumber", value);
			this.OnPropertyChanged("ImportSequenceNumber");
		}
	}
	
	/// <summary>
	/// Unique identifier of the user who modified the record.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("modifiedby")]
	public Microsoft.Xrm.Sdk.EntityReference ModifiedBy
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("modifiedby");
		}
	}
	
	/// <summary>
	/// Date and time when the record was modified.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("modifiedon")]
	public System.Nullable<System.DateTime> ModifiedOn
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<System.DateTime>>("modifiedon");
		}
	}
	
	/// <summary>
	/// Unique identifier of the delegate user who modified the record.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("modifiedonbehalfby")]
	public Microsoft.Xrm.Sdk.EntityReference ModifiedOnBehalfBy
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("modifiedonbehalfby");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("ModifiedOnBehalfBy");
			this.SetAttributeValue("modifiedonbehalfby", value);
			this.OnPropertyChanged("ModifiedOnBehalfBy");
		}
	}
	
	/// <summary>
	/// Unique identifier for the organization
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("organizationid")]
	public Microsoft.Xrm.Sdk.EntityReference OrganizationId
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("organizationid");
		}
	}
	
	/// <summary>
	/// Date and time that the record was migrated.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("overriddencreatedon")]
	public System.Nullable<System.DateTime> OverriddenCreatedOn
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<System.DateTime>>("overriddencreatedon");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("OverriddenCreatedOn");
			this.SetAttributeValue("overriddencreatedon", value);
			this.OnPropertyChanged("OverriddenCreatedOn");
		}
	}
	
	/// <summary>
	/// Status of the School
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("statecode")]
	public System.Nullable<CrmDemo.CrmDomainModel.Entities.idoe_schoolState> StateCode
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("statecode");
			if ((optionSet != null))
			{
				return ((CrmDemo.CrmDomainModel.Entities.idoe_schoolState)(System.Enum.ToObject(typeof(CrmDemo.CrmDomainModel.Entities.idoe_schoolState), optionSet.Value)));
			}
			else
			{
				return null;
			}
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("StateCode");
			if ((value == null))
			{
				this.SetAttributeValue("statecode", null);
			}
			else
			{
				this.SetAttributeValue("statecode", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));
			}
			this.OnPropertyChanged("StateCode");
		}
	}
	
	/// <summary>
	/// Reason for the status of the School
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("statuscode")]
	public Microsoft.Xrm.Sdk.OptionSetValue StatusCode
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("statuscode");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("StatusCode");
			this.SetAttributeValue("statuscode", value);
			this.OnPropertyChanged("StatusCode");
		}
	}
	
	/// <summary>
	/// For internal use only.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("timezoneruleversionnumber")]
	public System.Nullable<int> TimeZoneRuleVersionNumber
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<int>>("timezoneruleversionnumber");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("TimeZoneRuleVersionNumber");
			this.SetAttributeValue("timezoneruleversionnumber", value);
			this.OnPropertyChanged("TimeZoneRuleVersionNumber");
		}
	}
	
	/// <summary>
	/// Time zone code that was in use when the record was created.
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("utcconversiontimezonecode")]
	public System.Nullable<int> UTCConversionTimeZoneCode
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<int>>("utcconversiontimezonecode");
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("UTCConversionTimeZoneCode");
			this.SetAttributeValue("utcconversiontimezonecode", value);
			this.OnPropertyChanged("UTCConversionTimeZoneCode");
		}
	}
	
	/// <summary>
	/// Version Number
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("versionnumber")]
	public System.Nullable<long> VersionNumber
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetAttributeValue<System.Nullable<long>>("versionnumber");
		}
	}
	
	/// <summary>
	/// 1:N idoe_idoe_school_idoe_permission
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_idoe_school_idoe_permission")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.idoe_permission> idoe_idoe_school_idoe_permission
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.idoe_permission>("idoe_idoe_school_idoe_permission", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_idoe_school_idoe_permission");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.idoe_permission>("idoe_idoe_school_idoe_permission", null, value);
			this.OnPropertyChanged("idoe_idoe_school_idoe_permission");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_AsyncOperations
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_AsyncOperations")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.AsyncOperation> idoe_school_AsyncOperations
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.AsyncOperation>("idoe_school_AsyncOperations", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_AsyncOperations");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.AsyncOperation>("idoe_school_AsyncOperations", null, value);
			this.OnPropertyChanged("idoe_school_AsyncOperations");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_BulkDeleteFailures
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_BulkDeleteFailures")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.BulkDeleteFailure> idoe_school_BulkDeleteFailures
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.BulkDeleteFailure>("idoe_school_BulkDeleteFailures", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_BulkDeleteFailures");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.BulkDeleteFailure>("idoe_school_BulkDeleteFailures", null, value);
			this.OnPropertyChanged("idoe_school_BulkDeleteFailures");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_DuplicateBaseRecord
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_DuplicateBaseRecord")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.DuplicateRecord> idoe_school_DuplicateBaseRecord
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.DuplicateRecord>("idoe_school_DuplicateBaseRecord", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_DuplicateBaseRecord");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.DuplicateRecord>("idoe_school_DuplicateBaseRecord", null, value);
			this.OnPropertyChanged("idoe_school_DuplicateBaseRecord");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_DuplicateMatchingRecord
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_DuplicateMatchingRecord")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.DuplicateRecord> idoe_school_DuplicateMatchingRecord
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.DuplicateRecord>("idoe_school_DuplicateMatchingRecord", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_DuplicateMatchingRecord");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.DuplicateRecord>("idoe_school_DuplicateMatchingRecord", null, value);
			this.OnPropertyChanged("idoe_school_DuplicateMatchingRecord");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_MailboxTrackingFolders
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_MailboxTrackingFolders")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.MailboxTrackingFolder> idoe_school_MailboxTrackingFolders
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.MailboxTrackingFolder>("idoe_school_MailboxTrackingFolders", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_MailboxTrackingFolders");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.MailboxTrackingFolder>("idoe_school_MailboxTrackingFolders", null, value);
			this.OnPropertyChanged("idoe_school_MailboxTrackingFolders");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_PrincipalObjectAttributeAccesses
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_PrincipalObjectAttributeAccesses")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.PrincipalObjectAttributeAccess> idoe_school_PrincipalObjectAttributeAccesses
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.PrincipalObjectAttributeAccess>("idoe_school_PrincipalObjectAttributeAccesses", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_PrincipalObjectAttributeAccesses");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.PrincipalObjectAttributeAccess>("idoe_school_PrincipalObjectAttributeAccesses", null, value);
			this.OnPropertyChanged("idoe_school_PrincipalObjectAttributeAccesses");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_ProcessSession
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_ProcessSession")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.ProcessSession> idoe_school_ProcessSession
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.ProcessSession>("idoe_school_ProcessSession", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_ProcessSession");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.ProcessSession>("idoe_school_ProcessSession", null, value);
			this.OnPropertyChanged("idoe_school_ProcessSession");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_SyncErrors
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_SyncErrors")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.SyncError> idoe_school_SyncErrors
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.SyncError>("idoe_school_SyncErrors", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_SyncErrors");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.SyncError>("idoe_school_SyncErrors", null, value);
			this.OnPropertyChanged("idoe_school_SyncErrors");
		}
	}
	
	/// <summary>
	/// 1:N idoe_school_UserEntityInstanceDatas
	/// </summary>
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_school_UserEntityInstanceDatas")]
	public System.Collections.Generic.IEnumerable<CrmDemo.CrmDomainModel.Entities.UserEntityInstanceData> idoe_school_UserEntityInstanceDatas
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntities<CrmDemo.CrmDomainModel.Entities.UserEntityInstanceData>("idoe_school_UserEntityInstanceDatas", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_school_UserEntityInstanceDatas");
			this.SetRelatedEntities<CrmDemo.CrmDomainModel.Entities.UserEntityInstanceData>("idoe_school_UserEntityInstanceDatas", null, value);
			this.OnPropertyChanged("idoe_school_UserEntityInstanceDatas");
		}
	}
	
	/// <summary>
	/// N:1 idoe_idoe_activestatus_idoe_school_ActiveStatus
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_activestatus")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_idoe_activestatus_idoe_school_ActiveStatus")]
	public CrmDemo.CrmDomainModel.Entities.idoe_activestatus idoe_idoe_activestatus_idoe_school_ActiveStatus
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.idoe_activestatus>("idoe_idoe_activestatus_idoe_school_ActiveStatus", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_idoe_activestatus_idoe_school_ActiveStatus");
			this.SetRelatedEntity<CrmDemo.CrmDomainModel.Entities.idoe_activestatus>("idoe_idoe_activestatus_idoe_school_ActiveStatus", null, value);
			this.OnPropertyChanged("idoe_idoe_activestatus_idoe_school_ActiveStatus");
		}
	}
	
	/// <summary>
	/// N:1 idoe_idoe_corporation_idoe_school
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("idoe_corporationid")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("idoe_idoe_corporation_idoe_school")]
	public CrmDemo.CrmDomainModel.Entities.idoe_corporation idoe_idoe_corporation_idoe_school
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.idoe_corporation>("idoe_idoe_corporation_idoe_school", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("idoe_idoe_corporation_idoe_school");
			this.SetRelatedEntity<CrmDemo.CrmDomainModel.Entities.idoe_corporation>("idoe_idoe_corporation_idoe_school", null, value);
			this.OnPropertyChanged("idoe_idoe_corporation_idoe_school");
		}
	}
	
	/// <summary>
	/// N:1 lk_idoe_school_createdby
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdby")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_idoe_school_createdby")]
	public CrmDemo.CrmDomainModel.Entities.SystemUser lk_idoe_school_createdby
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_createdby", null);
		}
	}
	
	/// <summary>
	/// N:1 lk_idoe_school_createdonbehalfby
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("createdonbehalfby")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_idoe_school_createdonbehalfby")]
	public CrmDemo.CrmDomainModel.Entities.SystemUser lk_idoe_school_createdonbehalfby
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_createdonbehalfby", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("lk_idoe_school_createdonbehalfby");
			this.SetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_createdonbehalfby", null, value);
			this.OnPropertyChanged("lk_idoe_school_createdonbehalfby");
		}
	}
	
	/// <summary>
	/// N:1 lk_idoe_school_modifiedby
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("modifiedby")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_idoe_school_modifiedby")]
	public CrmDemo.CrmDomainModel.Entities.SystemUser lk_idoe_school_modifiedby
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_modifiedby", null);
		}
	}
	
	/// <summary>
	/// N:1 lk_idoe_school_modifiedonbehalfby
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("modifiedonbehalfby")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("lk_idoe_school_modifiedonbehalfby")]
	public CrmDemo.CrmDomainModel.Entities.SystemUser lk_idoe_school_modifiedonbehalfby
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_modifiedonbehalfby", null);
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			this.OnPropertyChanging("lk_idoe_school_modifiedonbehalfby");
			this.SetRelatedEntity<CrmDemo.CrmDomainModel.Entities.SystemUser>("lk_idoe_school_modifiedonbehalfby", null, value);
			this.OnPropertyChanged("lk_idoe_school_modifiedonbehalfby");
		}
	}
	
	/// <summary>
	/// N:1 organization_idoe_school
	/// </summary>
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("organizationid")]
	[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("organization_idoe_school")]
	public CrmDemo.CrmDomainModel.Entities.Organization organization_idoe_school
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return this.GetRelatedEntity<CrmDemo.CrmDomainModel.Entities.Organization>("organization_idoe_school", null);
		}
	}
	
	/// <summary>
	/// Constructor for populating via LINQ queries given a LINQ anonymous type
	/// <param name="anonymousType">LINQ anonymous type.</param>
	/// </summary>
	[System.Diagnostics.DebuggerNonUserCode()]
	public idoe_school(object anonymousType) : 
			this()
	{
        foreach (var p in anonymousType.GetType().GetProperties())
        {
            var value = p.GetValue(anonymousType, null);
            var name = p.Name.ToLower();
        
            if (name.EndsWith("enum") && value.GetType().BaseType == typeof(System.Enum))
            {
                value = new Microsoft.Xrm.Sdk.OptionSetValue((int) value);
                name = name.Remove(name.Length - "enum".Length);
            }
        
            switch (name)
            {
                case "id":
                    base.Id = (System.Guid)value;
                    Attributes["idoe_schoolid"] = base.Id;
                    break;
                case "idoe_schoolid":
                    var id = (System.Nullable<System.Guid>) value;
                    if(id == null){ continue; }
                    base.Id = id.Value;
                    Attributes[name] = base.Id;
                    break;
                case "formattedvalues":
                    // Add Support for FormattedValues
                    FormattedValues.AddRange((Microsoft.Xrm.Sdk.FormattedValueCollection)value);
                    break;
                default:
                    Attributes[name] = value;
                    break;
            }
        }
	}
	
	[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("statuscode")]
	public virtual idoe_school_StatusCode? StatusCodeEnum
	{
		[System.Diagnostics.DebuggerNonUserCode()]
		get
		{
			return ((idoe_school_StatusCode?)(EntityOptionSetEnum.GetEnum(this, "statuscode")));
		}
		[System.Diagnostics.DebuggerNonUserCode()]
		set
		{
			StatusCode = value.HasValue ? new Microsoft.Xrm.Sdk.OptionSetValue((int)value) : null;
		}
	}
}
}

And here is my DTO.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrmDemo.ServiceModel.Types
{
    public class idoe_school
    {
       public Guid idoe_schoolId { get; set; }
       public string idoe_name { get; set; }
       public string idoe_SchoolNumber { get; set; }

       public Microsoft.Xrm.Sdk.EntityReference idoe_CorporationId { get; set; }
    }
}

And here is the code that does the conversion of the CRM object to DTO

            EntityCollection ec = serviceProxy.RetrieveMultiple(qe);

        var response = new PostSchoolsByUserResponse();
        response.Schools = ec.Entities.ToList().ConvertAll(entity => entity.ConvertTo<CrmDemo.ServiceModel.Types.idoe_school>());
        return response;

I am a bit baffled at this point. I would love to use service stack as the go-between the two systems, my last hurdle is this serialization issue.

The generated code for Entities is horrible, it’s nowhere a clean POCO/DTO. It’s likely using something unsupported by the Serializers. If you want to use ServiceStack serializers copy the data from the generated classes into the clean DTO so you’re not exposing any generated entities in the DTO.

Is there an example of registering custom serializer for each entity type?

I guess, I am looking for a custom mapping not serializer.

Our built-in Auto Mapping isn’t configurable the way AutoMapper is, when a Type doesn’t map cleanly to another we use extension methods to handle any custom cases.

Thanks.

I will look into using AutoMapper, as it has worked well for me in the past.