所以我需要制作对象的副本。我在这里有一个模型“地方”,它有一个痛苦的 IList HasAndBelongsToMany 属性。我需要获取字段属性并复制它,但它仅复制参考。这是我所拥有的
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; }
}
}
并像这样使用自动映射器
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();
哪个有效,因为我正在跳过该领域。我所希望的更像是:
Mapper.CreateMap<place, place>().ForMember(dest => dest.id, o => o.Ignore())
.ForMember(dest => dest.field.id, o => o.Ignore())
;
请参阅 .ForMember(dest => dest.id, o => o.Ignore()) 的第一行,这样我就不会复制地点对象的参考 ID。我需要对 place 属性字段执行相同的操作。我需要忽略 id 并在其其余属性上创建具有相同值的新条目