我有生成的实体类(如MyClass),我需要读取一个csv文件,然后返回MyClass的List。在所有实体上,我们都有一个readonly属性readOn。要设置CreatedOn,我们需要执行以下自定义代码:属性[“overriddencreatedon”] = DateTime.Now;
有没有可能使用CsvHelper来做这样的事情?:
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"));
}
}
1条答案
按热度按时间mum43rcc1#
这应该适用于
Dictionary<string, object> Attributes
。如果
MyClass
继承自Microsoft.Xrm.Sdk.Entity
,那么它应该是AttributeCollection Attributes
。