1

I am using the CSVHelper framework to push data into Salesforce via the Salesforce Bulk API. The application is a simple C# Console app using Entity Framework. I'm currently working with the Account object only but will be adding other tables/objects so I want to make the code as reusable as possible.

One of my challenges is there are fields in the source table that I need to ignore using the CSVHelper [Ignore] attribute. This Key field is ignored so long as I directly place the attribute on the auto-generated Entity class property like so:

public partial class Account
{
    [CsvHelper.Configuration.Attributes.Ignore]
    public long Key { get; set; }
    // ...
}

But, this is not preferred since this file be regenerated when I add other tables/objects to the program via the Entity Designer.

So the first solution I tried was to simply create another partial class with a different file name but same class signature and property:

public partial class Account
{
  [CsvHelper.Configuration.Attributes.Ignore]
  public long Key { get; set; }
}

However, this would not compile since you cannot use the same property in separate partial file to define a property attribute:

enter image description here

So I did some research and I found several options to avoid placing the attribute directly on the auto-generated Entity code.

Method 1:

[MetadataType(typeof(Account.MetaData))]
public partial class Account
{
    internal class MetaData
    {
        [CsvHelper.Configuration.Attributes.Ignore]
        long Key { get; set; }
    }
}

While this compiles, the [Internal] attribute does not register and code does not ignore the Key field.

Method 2:

public interface IEntityMetadata
{
    [CsvHelper.Configuration.Attributes.Ignore]
    long Key { get; set; }
}

[MetadataType(typeof(IEntityMetadata))]
public partial class Account : IEntityMetadata
{
}

Again, the code compiles, but the [Internal] attribute does not register and code does not ignore the Key field.

Method 3:

[MetadataType(typeof(AccountAttributes))]
public partial class Account
{ }

public class AccountAttributes
{
    [CsvHelper.Configuration.Attributes.Ignore]
    public long Key { get; set; }
}

Just like the previous two methods, the code compiles, but the [Internal] attribute does not register and code does not ignore the Key field.

Only when the [Ignore] attribute is applied directly to the auto-generated Entity Account class property will the code ignore the Key field.

My research suggests that any of the 3 methods I used should have accomplished my goal. What might I be missing that is preventing these methods from working?

Thank you.

Swisher Sweet
  • 769
  • 11
  • 33

0 Answers0