This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, Consider the following class:
[Serializable] [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")] [System.ComponentModel.DataObject] [System.Runtime.Serialization.DataContract] public partial class Option : Entity<long> { #region Fields
[Column(ConverterType=typeof(ObjectConvertor))] private Object _value; [ValidateLength(0, 80)] private string _name; [Column(ConverterType=typeof(ObjectConvertor))] private Object _defaultValue; [ValidateLength(0, 128)] private string _label; [ValidateLength(0, 4096)] private string _description; [Column(ConverterType=typeof(TypeConvertor))] [ValidateLength(0, 255)] private System.Type _valueType; [DependentAttribute(ValidatePresence=false)] private System.Nullable<long> _partyId;
#pragma warning disable 649 // "Field is never assigned to" - LightSpeed assigns these fields internally [System.Runtime.Serialization.DataMember] private System.DateTime _createdOn; [System.Runtime.Serialization.DataMember] private System.DateTime _updatedOn; [System.Runtime.Serialization.DataMember] private System.Nullable<System.DateTime> _deletedOn; #pragma warning restore 649
#endregion
#region Field attribute and view names
/// <summary>Identifies the Value entity attribute.</summary> public const string ValueField = "Value"; /// <summary>Identifies the Name entity attribute.</summary> public const string NameField = "Name"; /// <summary>Identifies the DefaultValue entity attribute.</summary> public const string DefaultValueField = "DefaultValue"; /// <summary>Identifies the Label entity attribute.</summary> public const string LabelField = "Label"; /// <summary>Identifies the Description entity attribute.</summary> public const string DescriptionField = "Description"; /// <summary>Identifies the ValueType entity attribute.</summary> public const string ValueTypeField = "ValueType"; /// <summary>Identifies the PartyId entity attribute.</summary> public const string PartyIdField = "PartyId";
#endregion
#region Relationships
[ReverseAssociation("Options")] private readonly EntityHolder<Party> _party = new EntityHolder<Party>();
#endregion
#region Properties
[System.Diagnostics.DebuggerNonUserCode] public Party Party { get { return Get(_party); } set { Set(_party, value); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public Object Value { get { return Get(ref _value, "Value"); } set { Set(ref _value, value, "Value"); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public string Name { get { return Get(ref _name, "Name"); } set { Set(ref _name, value, "Name"); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public Object DefaultValue { get { return Get(ref _defaultValue, "DefaultValue"); } set { Set(ref _defaultValue, value, "DefaultValue"); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public string Label { get { return Get(ref _label, "Label"); } set { Set(ref _label, value, "Label"); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public string Description { get { return Get(ref _description, "Description"); } set { Set(ref _description, value, "Description"); } }
[System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public System.Type ValueType { get { return Get(ref _valueType, "ValueType"); } set { Set(ref _valueType, value, "ValueType"); } }
/// <summary>Gets or sets the ID for the <see cref="Party" /> property.</summary> [System.Runtime.Serialization.DataMember] [System.Diagnostics.DebuggerNonUserCode] public System.Nullable<long> PartyId { get { return Get(ref _partyId, "PartyId"); } set { Set(ref _partyId, value, "PartyId"); } } /// <summary>Gets the time the entity was created</summary> [System.Diagnostics.DebuggerNonUserCode] public System.DateTime CreatedOn { get { return _createdOn; } }
/// <summary>Gets the time the entity was last updated</summary> [System.Diagnostics.DebuggerNonUserCode] public System.DateTime UpdatedOn { get { return _updatedOn; } }
/// <summary>Gets the time the entity was soft-deleted</summary> [System.Diagnostics.DebuggerNonUserCode] public System.Nullable<System.DateTime> DeletedOn { get { return _deletedOn; } }
#endregion } The classes for TypeConvertor and ObjectConvertor is as follows:
public class ObjectConvertor : FieldConverter<byte[], object> { protected override object ConvertFromDatabase(byte[] databaseValue) { if (databaseValue == null) { return null; }
IFormatter formatter = new BinaryFormatter(); using (var stream = new MemoryStream(databaseValue)) { return formatter.Deserialize(stream); } }
protected override byte[] ConvertToDatabase(object value) { if (value == null) { return null; }
IFormatter formatter = new BinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, value); return stream.ToArray(); } } } public class TypeConvertor : FieldConverter<string, Type> { protected override Type ConvertFromDatabase(string databaseValue) { if (string.IsNullOrEmpty(databaseValue)) { return null; } return Type.GetType(databaseValue); }
protected override string ConvertToDatabase(Type value) { if (value == null) { return null; } return value.AssemblyQualifiedName; } } Using the following code snippet, the Value, DefaultValue and ValueType fields are all null and the convertors are never called.
public static class OptionFactory { public static Option Create(string name, string description, object value) { if (value == null) throw new ArgumentNullException("value");
if (name == null) throw new ArgumentNullException("name"); if (name == null) throw new ArgumentNullException("label"); if (description == null) throw new ArgumentNullException("description"); var option = new Option(); option.Name = name; option.Label = name; option.Description = description; option.ValueType = value.GetType(); option.Value = value; option.DefaultValue = value; option.PartyId = null; return option; } } LightSpeedContext context = LightSpeedContextFactory.Create(); using (IUnitOfWork session = context.CreateUnitOfWork()) { // create options session.Add(OptionFactory.Create("Sample1", "A sample option", TimeSpan.FromDays(7))); session.SaveChanges(); } I have tried for a couple of hours to identify the problem without any success.
Thanks,
Werner
|
|
|
In your UDT, set Is Standard Data Type set to false. This will apply the ValueFieldAttribute to the relevant fields. We'll see if we can improve the way this is presented in the UI -- I agree it's not clear at the moment. |
|