Search code examples
.netasp.net-mvc-3export-to-csvfilehelpers

MVC 3 with filehelper


I use filehelper 2.9.9 at http://teamcity.codebetter.com/project.html?projectId=project41 to export my data to CSV format.
I use [FieldNotInFile] to ignore some field I don't want to export.
I also use MetadataType to avoid edit model when my database changes
Here's my codes:

public partial class book
{
    public long id { get; set; }
    public string book { get; set; }
    public virtual author author { get; set; }
}


[MetadataType(typeof(bookMetadata))]
[DelimitedRecord(",")]
public partial class book
{

}

public class bookMetadata
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [FieldNotInFile] //It don't work at all
    public long id;

    [DisplayName("Book")]
    public string book { get; set; }
    public author author;



    [FieldNotInFile] //It don't work at all
    private author _author;

    public author author
    {
        get { return _author; }
        set { _author = value; }
    }
}

My problems here is when I apply [FieldNotInFile] attribute in metadata, it doesn't work at all.
When I encapsulate field in model that is generated by database and apply attribute, it works perfectly. However every time the database changes, I must merge them by hand.

Any help is appreciated
Thank in advance


Solution

  • This is probably the limitation of FileHelper (it probably just uses reflection to find attributes on the serialized class and is not aware of metadata buddy class).

    Try creating a Model class for it, copy the data using AutoMapper and that apply your attribute to the Model class. Editing generated code is to error-prone, IMHO.