C# FileHelpers错误:字段:';k#u BackingField和#x27;类型:XXX不是系统类型,因此此字段需要CustomConverter

C# FileHelpers错误:字段:';k#u BackingField和#x27;类型:XXX不是系统类型,因此此字段需要CustomConverter,c#,asp.net-mvc,serialization,filehelpers,C#,Asp.net Mvc,Serialization,Filehelpers,我需要读取一个基于类型的CSV文件,该文件由我的MVC模型自动生成。模型如下所示: public partial class Merchant { public long Id { get; set; } public string Name { get; set; } public Nullable<int> Category { get; set; } public virtual MerchantCategory MerchantCategor

我需要读取一个基于类型的CSV文件,该文件由我的MVC模型自动生成。模型如下所示:

public partial class Merchant
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Nullable<int> Category { get; set; }

    public virtual MerchantCategory MerchantCategory { get; set; }
}
并获取以下异常:

“k__BackingField”字段的类型为:MerchantCategory,但不是系统类型,因此此字段需要CustomConverter(请查看文档了解更多信息)

实际上,我的导入根本不需要这个字段,所以我尝试在派生类中忽略它:

[DelimitedRecord(",")]
public class MerchantForImport : Merchant {
    [FieldHidden]
    new public MerchantCategory MerchantCategory;
}

var engine = new FileHelperEngine<MerchantForImport>();
[分隔符记录(“,”)]
公共类商品进口:商户{
[现场隐藏]
新公共商品分类商品分类;
}
var engine=new FileHelperEngine();
还是同样的错误。我根本不需要这个字段,我不想为它实现任何
FieldConverter
,我从来没有要求过这个
k_ubackingfield
,在我的代码中找不到它

我无法调用
FileHelperEngine.Options.RemoveField()
,因为异常是由构造函数引发的


那是从哪里来的?如何消除它?

我收到此错误,特别是因为我的对象(即商户)缺少源文件中存在的列。在实现缺少的列之前,我可以通过向对象类public string[]MyProperty{get;set;}添加一个新属性来解决这个问题。这项工作帮助我意识到缺少了一个专栏

i、 e

公共部分类商户 { 公共长id{get;set;} .. .. .. 公共字符串[]MyProperty{get;set;}
}

从设计的角度来看,我认为你走错了方向。您正试图将
商户
类用于两种不兼容的用途。相反,您应该有两个独立的类

FileHelpers是一个用于描述csv文件的库,以便您可以轻松导入它们。您应该有一个
MerchantFileSpec
来描述您的文件。它实际上不是一个合适的C类——它可能有:表示未使用列的伪字段;大量属性
[FieldNullValue]
[FieldQuoted]
[FieldConverter]
;它最适用于公共字段(FileHelpers限制,这不是C#best practice),等等。它是描述导入文件的方便语法。它应该不包含任何业务逻辑或特殊构造函数或支持字段。让它尽可能简单

然后,您可以生成独立的MVC
Merchant
类。其目的是按照MVC框架的要求,使用外键、ID等来描述商户

然后使用
FileHelperEngine
将记录读取到一个数组中,并将其映射到
Merchant
的可枚举项(通过Linq或类似的库)

比如:

/// Your MVC-generated class. Add methods, getters, setters, whatever.
/// FileHelpers doesn't use this class.
class Merchant
{
  public long Id { get; set; }
  public string Name { get; set; }
  public Nullable<int> Category { get; set; }

  public virtual MerchantCategory MerchantCategory { get; set; }
}

/// This is the class FileHelpers will use
/// This class describes the CSV file only. Stick to whatever
/// syntax conventions are required by FileHelpers.
[DelimitedRecord(";")]
class ProductMerchantFileSpec
{
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public long Id;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public string Name;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    // Handle non-US formats such as , decimal points
    // convert from inches to centimetres? 
    // you get the idea...
    [FieldConverter(MyCustomizedCategoryConverter)] // you get the idea
    public int Category;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<ProductMerchantFileSpec>();
        var productMerchantRecords = engine.ReadFile(filePath);
        var productMerchants = productMerchantRecords
            .Select(x => new Merchant() { Id = x.Id, Name = x.Name, Category = x.Category });
    }
}
///您的MVC生成的类。添加方法、getter、setter等等。
///FileHelpers不使用此类。
阶级商人
{
公共长Id{get;set;}
公共字符串名称{get;set;}
公共可空类别{get;set;}
公共虚拟商品类别商品类别{get;set;}
}
///这是FileHelpers将使用的类
///此类仅描述CSV文件。坚持到底
///FileHelper需要语法约定。
[分隔符(“;”)]
类别ProductMerchantFileSpec
{
[FieldQuoted(QuoteMode.OptionalForRead)]
公共长Id;
[FieldQuoted(QuoteMode.OptionalForRead)]
公共字符串名称;
[FieldQuoted(QuoteMode.OptionalForRead)]
//处理非美国格式,如小数点
//把英寸换算成厘米?
//你明白了。。。
[FieldConverter(MyCustomizedCategoryConverter)]//你明白了
公共int类别;
}
班级计划
{
静态void Main(字符串[]参数)
{
var engine=new FileHelperEngine();
var productMerchantRecords=engine.ReadFile(文件路径);
var productMerchants=productMerchantRecords
.Select(x=>newmerchant(){Id=x.Id,Name=x.Name,Category=x.Category});
}
}

是的,我已经接受了你的方法。遗憾的是,我不能绑定到自动生成的类型,因为每当数据库中的表发生更改时,我都必须手动修改代码,以确保模式一致。好吧,这不会是每天的例行公事,所以我想没关系。我不能评论,所以我会这样添加一个注释@shamp00在他的回答中做出了这样的评论:>它最适用于公共字段(FileHelpers限制,这不是C#最佳实践),我注意到这是真的。如果我使用“internal”定义类和成员,那么如果我使用
FileHelperEngine.GetFileHeader()
头将显示错误。例如,如果其中一个字段是“Address”,则标题将显示:
k_ubackingfield
我必须更改类和成员定义以使用public才能停止该操作。
/// Your MVC-generated class. Add methods, getters, setters, whatever.
/// FileHelpers doesn't use this class.
class Merchant
{
  public long Id { get; set; }
  public string Name { get; set; }
  public Nullable<int> Category { get; set; }

  public virtual MerchantCategory MerchantCategory { get; set; }
}

/// This is the class FileHelpers will use
/// This class describes the CSV file only. Stick to whatever
/// syntax conventions are required by FileHelpers.
[DelimitedRecord(";")]
class ProductMerchantFileSpec
{
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public long Id;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public string Name;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    // Handle non-US formats such as , decimal points
    // convert from inches to centimetres? 
    // you get the idea...
    [FieldConverter(MyCustomizedCategoryConverter)] // you get the idea
    public int Category;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<ProductMerchantFileSpec>();
        var productMerchantRecords = engine.ReadFile(filePath);
        var productMerchants = productMerchantRecords
            .Select(x => new Merchant() { Id = x.Id, Name = x.Name, Category = x.Category });
    }
}