C# 使用Automapper创建具有IList属性的对象的完整副本

C# 使用Automapper创建具有IList属性的对象的完整副本,c#,automapper,asp.net-4.0,castle,C#,Automapper,Asp.net 4.0,Castle,所以我需要复制对象。我这里有一个“地方”模型,它有一个IList Hasandbelongtomany属性,这是一个痛苦。我需要获取字段属性并复制它,但它只复制引用。这是我的 public class place : ActiveRecordBase<place> { public place() { } private int place_id; [PrimaryKey("place_id")] virtual public int id

所以我需要复制对象。我这里有一个“地方”模型,它有一个IList Hasandbelongtomany属性,这是一个痛苦。我需要获取字段属性并复制它,但它只复制引用。这是我的

public class place : ActiveRecordBase<place>
{
    public place() {  }

    private int place_id;
    [PrimaryKey("place_id")]
    virtual public int id
    {
        get { return place_id; }
        set { place_id = value; }
    }
    private IList<fields> Fields;
    [HasAndBelongsToMany(typeof(fields), Lazy = true, Table = "place_to_fields", ColumnKey = "place_id", ColumnRef = "field_id", NotFoundBehaviour = NotFoundBehaviour.Ignore, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
    virtual public IList<fields> field
    {
        get { return Fields; }
        set { Fields = value; }
    }
}
公共类位置:ActiveRecordBase
{
公共场所(){}
私人内部场所标识;
[主密钥(“位置id”)]
虚拟公共整数id
{
获取{return place_id;}
设置{place_id=value;}
}
私有IList字段;
[hasandbelongtomany(字段类型,Lazy=true,Table=“place\u to\u fields”,ColumnKey=“place\u id”,ColumnRef=“field\u id”,notfoundbehavior=notfoundbehavior.Ignore,Cascade=ManyRelationCascadeEnum.alldeleteOrven]
虚拟公共IList字段
{
获取{返回字段;}
设置{Fields=value;}
}
}
像这样使用automapper

place org = ActiveRecordBase<place>.Find(id);

Mapper.Reset();
Mapper.CreateMap<place, place>().ForMember(dest => dest.id, o => o.Ignore())
                                .ForMember(dest => dest.field, o => o.Ignore())
                                ; 
place copy = new place();
Mapper.Map(org, copy);

copy.SaveAndFlush();
placeorg=ActiveRecordBase.Find(id);
Reset();
Mapper.CreateMap().formMember(dest=>dest.id,o=>o.Ignore())
.FormMember(dest=>dest.field,o=>o.Ignore())
; 
放置副本=新位置();
Mapper.Map(组织,副本);
copy.SaveAndFlush();
这很有效,因为我正在跳过场地。我所希望的更像是:

Mapper.CreateMap<place, place>().ForMember(dest => dest.id, o => o.Ignore())
                                .ForMember(dest => dest.field.id, o => o.Ignore())
                                ; 
Mapper.CreateMap().formMember(dest=>dest.id,o=>o.Ignore())
.FormMember(dest=>dest.field.id,o=>o.Ignore())
; 

请参阅.ForMember(dest=>dest.id,o=>o.Ignore())的第一行,这样我就不会复制place对象的引用id。我需要对place属性字段执行相同的操作。我需要忽略id并在其其余属性上创建具有相同值的新条目

您需要为字段类型创建映射,并为字段id添加“忽略”选项,就像您以前为place type所做的那样

Mapper.CreateMap<fields, fields>().ForMember(dest => dest.id, o => o.Ignore());
Mapper.CreateMap<place, place>().ForMember(dest => dest.id, o => o.Ignore());
Mapper.CreateMap().formMember(dest=>dest.id,o=>o.Ignore());
Mapper.CreateMap().formMember(dest=>dest.id,o=>o.Ignore());

因此,将place属性的映射作为IList放在上方或下方不会复制place对象中的字段。你不会错的。我本以为它必须嵌套才能工作?