Tag: #automapper

自动映射器找不到未映射的成员

我们正在使用Automapper进行项目,并且似乎随机获得以下错误: AutoMapper.AutoMapperConfigurationException:找到未映射的成员。 查看下面的类型和成员。 添加自定义映射表达式,忽略,添加自定义解析程序或修改源/目标类型 代码在几个月内没有改变。 我收到错误,刷新,错误消失,页面工作正常。 我正在使用 Mapper.AssertConfigurationIsValid(); 不知道为什么它抱怨映射不好然后刷新再次没问题,有没有人碰到这个? 调试没有帮助,因为它是随机的,有时没有错误,然后其他日子它将在网站的某个地方弹出,回到它并且没关系。 错误也出现在随机页面上,而不是相同的页面,而不是相同的映射。

如何配置Automapper以自动忽略具有ReadOnly属性的属性?

语境: 假设我有以下“目的地”类: public class Destination { public String WritableProperty { get; set; } public String ReadOnlyProperty { get; set; } } 和一个“source”类,其中一个属性具有ReadOnly属性: public class Source { public String WritableProperty { get; set; } [ReadOnly(true)] public String ReadOnlyProperty { get; set; } } 很明显,但要明确:我将以下列方式从Source类映射到Destination类: Mapper.Map(source, destination); 问题: 有哪些方法可以将Automapper配置为使用ReadOnly(true)属性自动忽略属性? 约束: 我使用Automapper的Profile类进行配置。 我不想弄脏具有Automapper特定属性的类。 我不想为每个只读属性配置Automapper,并且通过这种方式导致大量重复。 可能的(但不适合)解决方案: 1)将属性IgnoreMap添加到属性: [ReadOnly(true)] [IgnoreMap] […]

AutoMapper.Map忽略源对象的所有Null值属性

我正在尝试映射2个相同类型的对象。 我想要做的是AutoMapper to toigonore所有属性,在源对象中具有Null值,并保留目标对象中的现有值。 我已经尝试在我的“存储库”中使用它,但它似乎不起作用。 Mapper.CreateMap().ForAllMembers(p => p.Condition(c => !c.IsSourceValueNull)); 可能是什么问题?

将属性映射到集合项

我一直在筛选AutoMapper文档,试图找到一个推荐的解决方案,但一直无法找到它。 假设我有类似以下的课程 public class Foo { public string Note { get; set; } } 此类从客户端填充并映射到以下域对象类 public class Bar { public IList Notes { get; set; } } 注意的地方 public class Note { public string Text { get; set; } // other properties excluded for brevity } 我想在Foo上映射Note字符串属性,首先映射到新的Note实例上的Text属性,然后将该Note添加到Bar上的Notes集合中。 我正在使用ValueResolver来执行此操作的第一部分(将字符串映射到Note的新实例),但我不确定如何处理第二部分(将该项目映射到集合中的项目)。 这样做最干净的方法是什么?

.net中的对象复制方法:自动映射器,发射映射器,隐式操作,属性复制

如果有人知道在.NET中执行此操作的更多方法,您对此方法的看法是什么? 您选择哪种方法?为什么? 以下是.NET中不同对象复制方式的测试。 与此原始线程相关的测试: 如何使用c#中的相同属性名称将值从类X复制到类Y? 所以,在这里,你可以自己运行它: static void Main(string[] args) { Student _student = new Student(); _student.Id = 1; _student.Name = “Timmmmmmmmaaaahhhh”; _student.Courses = new List(); _student.Courses.Add(101); _student.Courses.Add(121); Stopwatch sw = new Stopwatch(); Mapper.CreateMap(); StartTest(sw, “Auto Mapper”); for (int i = 0; i < 1000000; i++) { StudentDTO dto = Mapper.Map(_student); } StopTest(sw); StartTest(sw, “Implicit […]

ASP.net MVC – 我应该使用ViewModel中的AutoMapper到Entity Framework实体吗?

我目前正在使用AutoMapper将我的Entity Framework实体映射到我的View Model: public class ProductsController : Controller { private IProductRepository productRepository; public ProductsController(IProductRepository productRepository) { this.productRepository = productRepository; } public ActionResult Details(int id) { var product = productRepository.GetProduct(id); if( product == null ) return View(“NotFound”); ProductDetailsViewModel model = Mapper.Map(product); return View(model); } } 这很好用。 我的问题是当我需要从我的View Model转到我的实体以更新数据库时。 我应该使用AutoMapper吗? 这是一个糟糕/危险的做法吗? 看起来AutoMapper很适合将复杂类型展平为简单(平面)类型,但到目前为止,我正在努力尝试从平面/简单到更复杂的类型,例如具有各种导航属性的实体。 如果使用AutoMapper执行此操作是个坏主意,那么我的代码对于Create操作会是什么样子? public ActionResult Create(CreateProductViewModel model) […]

哪个更快:Automapper,Valuinjector还是手动映射? 每个人的速度到什么程度?

假设我的DAL(ORM等)中有这个对象 public class Student { public string Name {get;set;} public string Address {get;set;} public string Phone {get;set;} public Parent Parent {get;set;} } public class Parent { public string Name {get;set;} public string Address {get;set;} public string Phone {get;set;} } 我有一个看起来像这样的ViewModel public class StudentDetailVM { public string Name {get;set;} public string Address {get;set;} public string Phone […]

Automapper说Mapper.Map是过时的全局映射?

我在我的项目中定义了一个全局Automapper配置,允许我使用Mapper.Map(sourceObject); 在我的代码中。 (参见下面的配置。) 我更新了NuGet包,我看到Mapper.Map已经过时/折旧的消息。 我在GitHub上回到Automapper,看到这样的例子: [Test] public void Example() { var config = new MapperConfiguration(cfg => { cfg.CreateMap().FixRootDest(); cfg.CreateMap().FixRootDest(); }); config.AssertConfigurationIsValid(); var mapper = config.CreateMapper(); var subDest1 = mapper.Map(new Source1 {SomeValue = “Value1”}); var subDest2 = mapper.Map(new Source2 {SomeOtherValue = “Value2”}); subDest1.SomeValue.ShouldEqual(“Value1”); subDest2.SomeOtherValue.ShouldEqual(“Value2”); } 我是否必须在使用映射的EVERY方法中创建配置? 我目前的全局配置: namespace PublicationSystem.App_Start { public class AutoMapperConfig { public static […]

当属性名称不同时使用Automapper

我们正在使用Codeplex的AutoMapper,对我来说,目标对象的所有属性都以’Field’结尾,即cityField,源对象只有city。 我可以使用下面的代码来实现,但所有的属性都只是’Field’的后缀,有20个属性。 .ForMember(dest => dest.cityField, opt => opt.MapFrom(origin => origin.City)); 是否有任何其他方法可以在映射时忽略“字段”字,以便它可以在不使用.ForMember()的情况下映射20次?

如何在AutoMapper映射中忽略属性的属性?

映像具有多对多关系的Person和Group类。 一个人有一个组列表,一个组有一个人员列表。 将Person映射到PersonDTO我有一个stack overflow exception因为AutoMapper无法处理Person > Groups > Members > Groups > Members > … 所以这是示例代码: public class Person { public string Name { get; set; } public List Groups { get; set; } } public class Group { public string Name { get; set; } public List Members { get; set; } } public […]