我有以下对象结构:
/// <summary>
/// nested message instance provided by a business service
/// </summary>
public class Message
{
public string Subject { get; set; }
public DateTime CreationDate { get; set; }
public List<Message> Messages { get; set; }
}
我想将该对象映射到以下对象结构:
/// <
我试图用automapper在内部类中传输字符串的信息。怎样才能得到D的值?我的第一次尝试是:
C = new C { D = "123"}
这在网络小提琴中失败了,那么什么才是正确的方法呢?
是否可以将内部类映射到另一个对象,或者是否有必要更改映射以使其正常工作?
using System;
using AutoMapper;
public class Foo
{
public string A { get; set; }
public int B { get; set; }
public class C
{
public
在我的项目中,我试图使用automapper尽可能地按照约定将命令对象还原为域对象。
当我显式地映射映射配置文件中的两个成员时,它可以工作,但是 --我认为这也应该按照惯例工作。我创建了来演示最小的情况。
相关的问题最终会随着人们显式地添加映射而结束,但是这类问题违背了Automapper是为什么而构建的,并且与文档相矛盾,不是吗?它也不适用于扁平,所以反转地图是一条红鲱鱼,我认为。
映射
public class Mapping: Profile
{
public Mapping()
{
this.CreateMap<CreateSelectionCom
我是AutoMapper新手,除非我误解了,否则AutoMapper在映射到Dto时应该始终创建深拷贝。然而,下面的测试代码向我展示了它正在创建浅表副本。我在这里错过了什么?
映射Config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using Sandbox.Models;
namespace Sandbox.Core.Automapper
{
public static class AutoMapperWebConf
在以前的AutoMapper版本中,我曾经能够像这样配置AutoMapper:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembl
我试图使用LLBLGen在AutoMapper实体和DTO之间创建映射。
我的DTO看起来如下:
// Parent
public int Id { get; set; }
public List<Child> Children{ get; set; } // One to Many
// Child
public int Id { get; set; }
public int Parent { get; set; } // Foreign key to parent Id
ParentEntity包含一个与DTO列表名称相同的ChildCollection和一个Id (包含需要
我已经阅读了嵌套映射wiki页面,但它似乎不喜欢多级嵌套。我已经创建了以下映射并定义了类。
AutoMapper.Mapper.CreateMap<Address, AddressDTO>();
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>();
public class MatchCompanyRequest
{
Address Address {get;set;}
}
public class MatchCompanyRequestDTO
{
pu
我有一个名为Employee的视图模型类和一个名为Employees的EF实体。如果我写下面的查询,我得到一个错误,声明“无法转换类型,显式转换已经存在)我知道如何使用select new ViewModel.Employee子句来解决这个问题,但是我想知道是否有任何简明的方法来解决这个问题?
var selectedEmployee = (from q in emsCtx.Employees
where q.Id == employee.Id
在接下来的一两个星期里,我们的代码中开始出现一个奇怪的错误。我试图找出映射失败的根本原因。最内在的例外是令人费解的:Type 'System.String' does not have a default constructor。
我不明白什么是例外告诉我的。你能解释一下发生了什么吗?也许我可以解决这个问题?
映射器在泛型方法中使用:
public TEntity UpdateWithHistory<TEntity>(TEntity entity, int? entityID, int? interviewID)
where TEntity : class
{
我有三个相关实体A、B和C,其中A是B的父级,B是C的父级。
public class A
{
public int Id { get; set; }
public B B { get; set; }
}
public class B
{
public int Id { get; set; }
public C C { get; set; }
}
public class C
{
public int Id { get; set; }
我正在使用Automapper将EF对象映射到DTO,其中许多对象处于多到多的安排中。例如:
committee (table) 1 =< m committeemember (table) m >= 1 person (table)
它可以映射到:
public class CommitteeViewModel
{
public int idCommittee { get; set; }
public IEnumerable<CommitteeMemberViewModel> CommitteeMembers { get; set; }
}
pu
如果源对象为空,是否可以将AutoMapper配置为返回目标类型的新实例?
Source source = null;
Dest d1 = AutoMapper.Mapper.Map<Source, Dest>(source);
// d1 == null
// I'm looking for a way to configure AutoMapper to
// eliminate this code:
Dest d2 = AutoMapper.Mapper.Map<Source, Dest>(source) ?? new Dest();
如何将单个对象映射到嵌套对象?
下面一个是我的实体。
class Employee
{
public string name { get; set; }
public string city_name { get; set; }
public string State_name { get; set; }
}
我想映射到
class Employee
{
public string Name;
public Location Location;
}
class Location
{
public string City;
public
我有一个由12个实体框架对象组成的层次结构。
我还为每个实体创建了DTO。
我想通过电线发送DTO。
我得用DTO的方法。
您将如何使用Automapper映射此数量的对象?
我必须使用12倍的AutoMapper.Map方法吗?
更新
我现在明白了这个错误:
{"Missing type map configuration or unsupported mapping.\r\n\r\n....
I have an NumberEntity.cs with 3 complex properties which I want to map to
a NumberDTO.cs with
我在实体框架中使用AutoMapper。我有一个实体等级:
Person
学生
工人
每个业务对象都有一个到数据库中的实体的映射。要将业务对象转换为实体,我使用的是AutoMapper v6.2.2,我想知道是否有更好的方法来找到“最佳”的映射,还是真的需要这样的代码:
public PersonEntity MapPerson(Person person)
{
switch (person.Type)
{
case PersonType.Unknown:
r
我已经尝试了很多例子在这里和从自动维基,我仍然无法解决这个问题。我试图映射一个嵌套的对象和一个嵌套的集合,不管我做什么,它总是抛出一个错误。获得控制器返回数据的唯一方法是为这两个属性打开option.ignore。
这些是我试图映射的业务层对象。
public class LocationBL
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Address2 { get;
在迁移到v2.0之后,我发现了这个问题
我尝试像这样映射属性:
public class SurveyFormView
{
public MultiSelectList Statistics { get; set; }
}
public class SurveyForm
{
ICollection<Statistic> statistics = new List<Statistic>();
public virtual ICollection<Statistic> Statistics
{
get { re
当我试图映射两个类时,我会得到以下异常。
AutoMapper.AutoMapperMappingException: 'Error mapping types.'
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
这是一个简单的、可重复的控制台应用程序,演示了这个问题:
using AutoMapper;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
我有一个模型,ViewModel是这样的
public class Estate : BaseEntity
{
public virtual BaseInformation floorType { get; set; }
}
public class BaseInformation:BaseEntity
{
public string Name { get; set; }
public virtual BaseInformationHeader Master { get; set; }
}
public class Estate
在AutoMapper 2.2.1中,是否可以配置映射,以便在不显式忽略属性时抛出异常?例如,我有以下类和配置:
public class Source
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
}
public class Destination
{
public int X { get; set; }
public int Y { get; set; }
}
// Config
Mapper.CreateMap<So
我有以下模式:
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
}
我希望能够使用AutoMapper将Name类型的Tag属性映射到一个视图模型中的string属性。
我已经创建了一个自定义解析器来尝试处理此映射,使用以下代码:
public class TagToStringResolver : ValueResolver<Tag, string>
{
protected override string ResolveCore(
有没有一种方法可以用AutoMapper映射静态类?
场景是这样的。有一个名为UserIdentity的静态类,它包含属性。我希望将UserIdentity类值映射到名为UserDTO的非静态类。
当我试着
AutoMapper.Mapper.CreateMap<UserIdentity, UserDTO>();
有一个错误说,
static type cannot be used as type arguments
现在,谁能给我解释一下这个场景,它能不能实现。有没有办法在不使用Automapper的情况下映射静态类?否则,我必须手动完成它们。
摘要:--我在调试一组AutoMapper映射时遇到了困难,需要一种更“外科”的方法来定位要修复的坏映射。
问题: InvalidOperationException中的异常消息是:
No coercion operator is defined between types 'My.Models.WorkViewModel' and 'My.Model.Work'.
抛出异常时的堆栈跟踪在调试器中是可见的,并且是简单和直接的,但没有太大帮助:
ex.StackTrace:
at AutoMapper.MapperConfiguration.Generate
我想把数据表映射到一个模型。我用AutoMapper来做这份工作。我用NUnit编写了单元测试。我就是这样做的
模型
public class DataTableModelTest
{
public int Dosage { get; set; }
public string Drug { get; set; }
}
Mapper法
class MapperClass{
public IList<TResultType> MapToModel<TResultType>(DataTable datatable)
{
AutoM
我目前正在评估AutoMapper是否对我们的项目有好处。我正在使用RESTful Web API开发一个ASP.NET Web API,我必须返回的内容之一是包含链接的资源。考虑这个简化的示例,使用下列域对象:
public class Customer
{
public string Name { get; set; }
}
我需要将它映射到一个资源对象中,有点像DTO,但添加了一些属性来促进REST。下面是我的资源对象可能的样子:
public class CustomerResource
{
public string Name { get; set; }
pu
我的AutoMapper配置有问题,我可以映射一个级别,但我不知道是否有一种方法可以映射到两个级别
我有:
Class A
{
public int id {get; set;}
public string nom {get; set;}
public B Bprop {get; set;}
}
Class B
{
public int id {get; set;}
public string nom {get; set;}
public C Cprop {get; set;}
}
Class C
{
public int id
我正在合并具有列表成员的对象。我告诉AutoMapper忽略null源成员,但是当我合并一个具有null集合的对象时,目标get是一个空集合(即使它在映射之前有项)。
有没有关于如何防止这种情况的想法?
ConfigurationInfo template1 = new ConfigurationInfo() {
Columns = null //1st templates list of columns is null
};
ConfigurationInfo template2 = new Configuration