I have generated entities classes (like MyClass), and I need to read a csv file, and then return a List of MyClass. On all entities, we have a readonly property CreateOn. To set the CreatedOn, we need to do this custom code : Attributes["overriddencreatedon"] = DateTime.Now;
Is it possible with CsvHelper to do something like this ? :
public class MyClass // in real life, inherits from Microsoft.Xrm.Sdk.Entity
{
public Guid Id { get; set; }
public DateTime CreatedOn { get;} // Readonly because generated class...
public Dictionary<string, object> Attributes { get; set; }
}
private class MyClassMap : ClassMap<MyClass>
{
public MyClassMap()
{
Map(f => f.Id).Name("Id");
// Cannot do this : Map(f => f.CreatedOn).Name("CreatedOn");
// Because CreatedOn is readonly
// The goal is to store the value in the dictionary Attributes, not in the property CreatedOn because it is readonly
// Instead, we need to do something like this:
Map().XXX((row, x) => x.Attributes["overriddencreatedon"] = row.Row.GetField<DateTime>("CreatedOn"));
}
}
This should work for Dictionary<string, object> Attributes
.
Map(f => f.Attributes).Convert(args =>
{
return new Dictionary<string, object> {
{ "overriddencreatedon", args.Row.GetField<DateTime>("CreatedOn") }
};
});
If MyClass
inherits from Microsoft.Xrm.Sdk.Entity
then it should be AttributeCollection Attributes
.
Map(f => f.Attributes).Convert(args =>
{
return new AttributeCollection {
{ "overriddencreatedon", args.Row.GetField<DateTime>("CreatedOn") }
};
});