我知道它是AutoMapper而不是AutoMerge(r),但......
我已经开始使用AutoMapper并且需要映射A - > B,并从C添加一些属性,以便B成为A + C的一种平面组合.
这是否可以在AutoMapper中使用AutoMapper来进行繁重的工作然后手动映射其他属性?
今天我使用AutoMapper v1.1升级了一个功能齐全的应用程序,现在使用AutoMapper v2.1,我遇到了一些我以前从未遇到的问题.
这是我的代码映射的示例从后DTO到域对象
public class TypeOne
{
public TypeOne()
{
}
public TypeOne(TypeTwo two)
{
//throw ex if two is null
}
public TypeOne(TypeTwo two, TypeThree three)
{
//throw ex if two or three are null
}
public TypeTwo Two {get; private set;}
public TypeThree Three {get; private set;}
}
public class TypeOneDto
{
public TypeOneDto()
{
}
public TypeTwoDto Two {get; set;}
public TypeThreeDto Three {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
...
Mapper.CreateMap<TypeThreeDto, TypeThree>();
Mapper.CreateMap<TypeTwoDto, TypeTwo>();
Mapper.CreateMap<TypeOneDto, …Run Code Online (Sandbox Code Playgroud) 我将映射定义定义为
Mapper.CreateMap<Calculator, CalculatorViewModel>()
.ForMember(dest => dest.TypeIndicator, src => src.ResolveUsing(new TypeIndicatorResolver()));
Run Code Online (Sandbox Code Playgroud)
我应该使用ResolveUsing还是MapFrom(src => SomePrivateMethod())?
ResolveUsing和MapFrom在复杂映射方面有什么区别.
Resolver或Private方法将转到数据库并获取值.
我在ASP.NET MVC 5应用程序中使用AutoMapper 6.2.0.
当我通过控制器调用我的视图时,它显示所有正确的事情.但是,当我刷新该视图时,Visual Studio显示错误:
System.InvalidOperationException:'Mapper已初始化.您必须为每个应用程序域/进程调用一次Initialize.
我只在一个控制器中使用AutoMapper.尚未在任何地方进行任何配置,也未在任何其他服务或控制器中使用AutoMapper.
我的控制器:
public class StudentsController : Controller
{
private DataContext db = new DataContext();
// GET: Students
public ActionResult Index([Form] QueryOptions queryOptions)
{
var students = db.Students.Include(s => s.Father);
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Student, StudentViewModel>();
});
return View(new ResulList<StudentViewModel> {
QueryOptions = queryOptions,
Model = AutoMapper.Mapper.Map<List<Student>,List<StudentViewModel>>(students.ToList())
});
}
// Other Methods are deleted for ease...
Run Code Online (Sandbox Code Playgroud)
控制器内出错:
我的Model类:
public class Student
{
[Key]
public int Id { get; set; }
public string Name { get; …Run Code Online (Sandbox Code Playgroud) 如果我想进行双向映射,是否需要创建两个映射?
Mapper.CreateMap<A, B>() and Mapper.CreateMap<B, A>()?
我使用AutoMapper从平面DataObjects映射到胖BusinessObjects,反之亦然.我注意到,由于BusinessObjects的更改通知(使用自定义验证实现INotifyPropertyChanged等),从DataObjects到BusinessObjects的映射需要额外的时间.
因为我在映射期间通常不需要更改通知,所以我想将其关闭.所以我添加了一个属性"IsPropertyChangedEnabled".如果此属性设置为false,则不会引发NotifyPropertyChanged事件并保存时间.
题:
我可以告诉AutoMapper在映射过程的最开始将此属性设置为false吗?如果是这样,怎么样?
谢谢!
https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API
这个改变打破了我的系统.
在更新之前,我使用:
===> Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
...
MyAutoMapperConfiguration.Configure();
}
}
Run Code Online (Sandbox Code Playgroud)
===> MyAutoMapperConfiguration.cs
public class MyAutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(a =>
{
a.AddProfile<AbcMappingProfile>();
a.AddProfile<XyzMappingProfile>();
a.AddProfile<QweMappingProfile>();
});
}
}
Run Code Online (Sandbox Code Playgroud)
===> AbcMappingProfile.cs
public class AbcMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<AbcEditViewModel, Abc>();
Mapper.CreateMap<Abc, AbcEditViewModel>();
...
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
'Mapper.CreateMap()'已过时:'静态API将在5.0版中删除.使用MapperConfiguration实例并根据需要静态存储.使用CreateMapper创建一个映射器实例.
我可以使用Mapper.Map.现在我该如何使用它
我在我的项目中定义了一个全局的Automapper配置,允许我Mapper.Map<targetType>(sourceObject);在我的代码中使用.(参见下面的配置.)
我更新了NuGet包,我看到了Mapper.Map已经过时/折旧的消息.我在GitHub上回到Automapper,看到这样的例子:
[Test]
public void Example()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source1, SubDest1>().FixRootDest();
cfg.CreateMap<Source2, SubDest2>().FixRootDest();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var subDest1 = mapper.Map<Source1, SubDest1>(new Source1 {SomeValue = "Value1"});
var subDest2 = mapper.Map<Source2, SubDest2>(new Source2 {SomeOtherValue = "Value2"});
subDest1.SomeValue.ShouldEqual("Value1");
subDest2.SomeOtherValue.ShouldEqual("Value2");
}
Run Code Online (Sandbox Code Playgroud)
我是否必须在使用映射的EVERY方法中创建配置?
我目前的全局配置:
namespace PublicationSystem.App_Start
{
public class AutoMapperConfig
{
public static void CreateMaps()
{
CreateProjectMaps();
}
private static void CreateProjectMaps()
{
Mapper.CreateMap<Project, ProjectCreate>();
Mapper.CreateMap<Project, ProjectSelectable>();
//...
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:感谢Scott Chamberlain的一些指导,我创建了一个这样的课程:
public …Run Code Online (Sandbox Code Playgroud) 假设我有以下"目的地"类:
public class Destination
{
public String WritableProperty { get; set; }
public String ReadOnlyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和一个"source"类,ReadOnly其中一个属性的属性:
public class Source
{
public String WritableProperty { get; set; }
[ReadOnly(true)]
public String ReadOnlyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
很明显,但要明确:我将按照以下方式从一个Source类映射到另一个Destination类:
Mapper.Map(source, destination);
Run Code Online (Sandbox Code Playgroud)
配置Automapper以自动忽略属性ReadOnly(true)属性的方法有哪些?
我使用Automapper的Profile类进行配置.我不想弄脏具有Automapper特定属性的类.我不想为每个只读属性配置Automapper,并且通过这种方式导致大量重复.
IgnoreMap向属性添加属性: [ReadOnly(true)]
[IgnoreMap]
public String ReadOnlyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
我不想使用特定于自动化程序的属性来弄脏类并使其依赖于它.另外,我不想在属性中添加其他ReadOnly属性.
CreateMap<Source, Destination>()
.ForSourceMember(src => …Run Code Online (Sandbox Code Playgroud) 在AutoMapper 8.0之前,我使用了以下代码:
CreateMap<ApplicationRole, RoleViewModel>()
.ForMember(d => d.Permissions, map => map.MapFrom(s => s.Claims))
.ForMember(d => d.UsersCount, map => map.ResolveUsing(s => s.Users?.Count ?? 0))
.ReverseMap();
Run Code Online (Sandbox Code Playgroud)
文档说你必须为MapFrom更改ResolveUsing,但我有一个错误"没有传播空"
.ForMember(d => d.UsersCount, map => map.MapFrom(s => s.Users?.Count ?? 0))
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
automapper ×10
c# ×5
asp.net ×2
asp.net-mvc ×2
.net ×1
asp.net-core ×1
automapper-2 ×1
automapper-6 ×1
mapping ×1