在我们更新到版本9之前,我可能有和奇怪的AutoMapper设置正在工作。
我们从数据库中获取数据,将其包装在一个通用对象中,然后将其映射到DTO:
//build some models
Domain test1 = new Domain() { Id = 1, Name = "Shaun" };
Domain test2 = new Domain() { Id = 2, Name = "Dave" };
//this is what I get from my API
Revi
public class BaseEntity
{
Public Int Id{get;set;}
Public DateTime CreateTime{get;set;}
Public DateTime UpdateTime{get;set;}
}
public class EntityA:BaseEntity
{
Public string PropA{get;set;}
}
Public class BaseEntityDto
{
Public Int Id{get;set;}
}
public class Entity
首先是一些背景知识..我正在使用Telerik MVC网格来显示数据。网格期望将一个IEnumerable传递给它们的GridModel类,我假设这个类负责页面、排序、过滤等。
为了避免在通过ajax将数据作为JSON传递时出现循环引用,我需要将EF linq查询的结果映射到View model objects...AutoMapper,这将是我选择的方法,但我想出的唯一涉及AutoMapper的解决方案会严重影响性能。
我有一个存储库,它将我的视图模型类型的IEnumerable返回给由网格调用的操作方法。
public IEnumerable<ResultViewModel>
我有一个简单的数据模型:
// Model
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
.... Another values here ....
}
public class Person
{
public string FirstName { get; set; }
我想使用AutoMapper合并两个对象(参见下面的测试)。我尝试了很多东西,但它总是从目标或源复制两个属性。
我怎么通过下面的考试?
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Test]
public void Merge_people()
{
var source = new Person() { LastName = "Smith" };
var target = new Perso
const observable$ = combineLatest([searchAPI$, searchByID$]) // Line 1
.pipe(
map(data => {
let APISearchResult = data[0]; // This is an array of SearchResult type
const searchByIDRawData = data[1]; // This is an array of Opportunity type
我的解决方案中有以下类:
public class VehicleMakeEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<VehicleModelEntity> VehicleModels { get; set; }
}
public class VehicleModelEntity
{
public Guid Id { get; set; }
这可能是一个简单的答案,但我才刚刚开始了解AutoMapper。
总之,我有这样一个域对象:
public class User
{
public string Name { get; set; }
public FacebookUser FacebookUser { get; set; }
}
像这样的ViewModel:
public class UserViewModel
{
public string Name { get; set; }
public long FacebookUniqueId { get; set; }
}
下面是我的AutoMapper配置
我有一个从DB获取数据的类,它看起来如下(为了保持它的简单性,缺少了一些字段):
public class BranchDetailDto
{
public BranchDetailDto()
{
}
public BranchDetailDto(int supplierId, string supplierName)
{
SupplierId = supplierId;
SupplierName = supplierName;
}
public int Id { get; set; }
pu
我想使用视图模型来显示域模型。我想定制一个显示的属性,我应该怎么做呢?使用AutoMapper进行显示是一个很好的实践吗?
下面是代码示例:
public class BookController : BaseController
{
private IBookService bookService;
public BookController(IBookService bookService)
{
this.bookService = bookService;
}
public
好的,我很难从一个模型转换为一个dto模型,反之亦然,这个模型是有效的,但是当试图映射"AutoMapper.AutoMapperMappingException‘发生在AutoMapper.dll“时
下面是模型类
public class Category
{
public Guid CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Post> Posts { get; set
我想用AutoMapper映射Order类:
class Order
{
public int Id { get; set; }
public int Quantity { get; set; }
public Product Product { get; set; }
}
产品类:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
我为Order类和Product创建了AutoM
AutoMapper没有本地方法来更新需要删除实例、添加或更新的嵌套列表吗?
我在使用EF的ASP.Net核心应用程序中使用ASP.Net将我的API资源映射到我的模型中。这对于我的大多数应用程序来说都很好,但是我对我的解决方案并不满意,因为我的解决方案是更新映射的嵌套列表,其中列出的实例需要持续存在。我不想覆盖现有列表,我希望删除不再在传入资源中的实例,添加新实例,并更新现有实例。
模型:
public class MainObject
{
public int Id { get; set; }
public List<SubItem> SubItems { ge
我试图使用DataContract将实体框架代码第一类映射到WCF AutoMapper类。
守则如下:
[DataContract]
public class User
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string Email {
在接下来的一两个星期里,我们的代码中开始出现一个奇怪的错误。我试图找出映射失败的根本原因。最内在的例外是令人费解的:Type 'System.String' does not have a default constructor。
我不明白什么是例外告诉我的。你能解释一下发生了什么吗?也许我可以解决这个问题?
映射器在泛型方法中使用:
public TEntity UpdateWithHistory<TEntity>(TEntity entity, int? entityID, int? interviewID)
where TEntity : class
{
在以前的AutoMapper版本中,我曾经能够像这样配置AutoMapper:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembl
我有两个类,并使用Automapper将一个类映射到另一个类。例如:
public class Source
{
// IdName is a simple class containing two fields: Id (int) and Name (string)
public IdName Type { get; set; }
public int TypeId {get; set; }
// another members
}
public class Destination
{
// IdNameDest is a simple cl
在迁移到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
我需要定制MyAutoMapper配置文件将我的对象映射到DTO的方式。从我的ApplicationServices中,我使用ObjectMapper进行相对简单的映射。问题是,ABP的AutoMapper并不是每个人都知道的正常AutoMapper。
下面是理想情况的一个片段,除了opt.MapFrom(m => Localizer[m.Type.ToString()])和_objectMapper.Map<Preparation, DtoPreparation>(preparation, _localizer)不能那样工作。
public class MyAutoMapp
如果源对象为空,是否可以将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();
我已经阅读了嵌套映射wiki页面,但它似乎不喜欢多级嵌套。我已经创建了以下映射并定义了类。
AutoMapper.Mapper.CreateMap<Address, AddressDTO>();
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>();
public class MatchCompanyRequest
{
Address Address {get;set;}
}
public class MatchCompanyRequestDTO
{
pu
我知道AutoMapper只是在两个对象之间映射属性。
Automapper不是用来重塑数据的,在我看来,这必须要编程。
我有一个需要重塑为PeriodListViewModel.List<CellViewModel>的PeriodDTO.IEnumerable<Period>。
每个CellViewModel包含一个List<RowViewModel>
public class PeriodRequest
{
public IEnumerable<Period> Periods { get; set; }