c# – Automapper中的行为不一致
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Automapper中的行为不一致,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2245字,纯文字阅读大概需要4分钟。
内容图文
![c# – Automapper中的行为不一致](/upload/InfoBanner/zyjiaocheng/816/b60fa59643f84dc1a4414691e134e18e.jpg)
在我的项目中发生了非常奇怪的事情,我有一个非常简单的CLR对象,
第一个是模型其他是ViewModel,在我编译项目后运行我的WebApi ASP.NET项目,使用所需的参数,我可以看到我的模型返回数据.
有一次我可以看到Mapper确实映射好了,第二次它返回所有带空值的东西.
问题不是一直发生的.
非常重要:更新于2013年3月14日
当我回收应用程序时,它停止这样做,
但过了一段时间它再次开始这样做,我重新保存web.config文件
然后它又好了.
这是我的Model / ViewModel:
public class Gallery : Entity
{
public override long Id { get; set; }
public virtual Settings SiteOwner { get; set; }
public virtual Category Category { get; set; }
public virtual string PageTitle { get; set; }
public virtual string TitleDescription { get; set; }
public virtual string GalleryTitle { get; set; }
public virtual IList<UIItem> GalleryItems { get; set; }
}
public class UIItem : Entity
{
public override long Id { get; set; }
public virtual Product Product { get; set; }
public virtual Gallery Gallery { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Price { get; set; }
public virtual string ImageUrl { get; set; }
public virtual string VideoUrl { get; set; }
public virtual string FileUrl { get; set; }
}
public class GalleryViewModel
{
public virtual string PageTitle { get; set; }
public virtual string TitleDescription { get; set; }
public virtual string GalleryTitle { get; set; }
public virtual IList<UIItemViewModel> GalleryItems { get; set; }
}
public class UIItemViewModel
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Price { get; set; }
public virtual string ImageUrl { get; set; }
public virtual string VideoUrl { get; set; }
public virtual string FileUrl { get; set; }
}
这是我使用它的方式
// my apicontroller
// FindGalleryByAppIdAndCategoryId returns => Gallery
var source = _galleryRepository.FindGalleryByAppIdAndCategoryId(appId, catId);
return Mapper.DynamicMap<GalleryViewModel>(source);
解决方法:
很可能这种情况发生在Gallery和UIItem有交叉引用的情况下:
public class Gallery : Entity
{
public virtual IList<UIItem> GalleryItems { get; set; }
}
public class UIItem : Entity
{
public virtual Gallery Gallery { get; set; }
}
你可以在你的代码中测试这个案例吗?
内容总结
以上是互联网集市为您收集整理的c# – Automapper中的行为不一致全部内容,希望文章能够帮你解决c# – Automapper中的行为不一致所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。