我的项目WebApi存在问题,使用AutoMapper。
在我的解决方案中,我使用AutoMapper v4.1.1,解决方案旧,内部有很多代码。
4个项目在哪里
在这个解决方案中我使用:
WebConfig中的连接字符串基于SqlServer的本地数据库。
我有两个模型,用户 - 其DataBaseEntity和 UserModel - 这是BussinesModel
我在DataLayer中的AutoMapper类看起来如此
namespace DataLayer
{
internal static class Mapping
{
static Mapping()
{
RegisterMappings();
}
public static TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
{
return Mapper.Map(source, destination);
}
private static void RegisterMappings()
{
try
{
Mapper.AllowNullDestinationValues = true;
//Mapping instruction for User
Mapper.CreateMap<User, UserModel>().IgnoreAllPropertiesWithAnInaccessibleSetter().ReverseMap();
Mapper.AssertConfigurationIsValid();
}
}
}
在CoreLayer中我使用此代码
public AuthenticationResult Login(string login, string password)
{
if (string.IsNullOrEmpty(login))
throw new ArgumentNullException(nameof(login));
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(nameof(password));
using (var db = new Context())
{
try
{
//Here my UserDbEntity model, and it already connected from Context
var dbUser = db.Users.AsNoTracking().FirstOrDefault(u => u.Login == login && u.IsActive && !u.IsDeleted);
//mapping model
var model = Mapping.Map(dbUser, new UserModel());
//Some result logic here
return result;
}
}
}
WebApi和WebFroms Mvc项目中对项目的所有引用都是相同的(对DataLayer和CoreLayer的引用)
现在问题,比我叫行动
CoreLayer.Login(string login, string pass)
从我的MVC项目中,他正确地返回了数据 如果我从Web Api项目中这样做,他会向我显示错误
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==================================================================
ServiceModel ->Service (Source member list)
Core.Models.ServiceModel -> Data.DB.Service (Source member list)
Unmapped properties:
Specialization
ServiceTechnic
ServiceTools
RefRescueWorkTypes
所有这些未映射的成员都是复杂的对象(模型内部的模型),并且有自己的映射规则。当我在MVC项目中工作时,他们正确地正确
P.S对不起我的英语,这不是我的母语
@Alexandru Marculescu的UPD 1 unmmaped模型的例子,
来源 个人信息实体
internal partial class PersonalInformation
{
public override System.Guid Id { get; set; }
public string FullName { get; set; }
//link to related Address entity
public Nullable<System.Guid> AddressId { get; set; }
public string EMail { get; set; }
// a lot of string properties here, nothing intresting
internal virtual Address Address { get; set; }
}
地址实体
internal partial class Address
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Address()
{
this.PersonalInformations = new HashSet<PersonalInformation>();
}
public override System.Guid Id { get; set; }
//hardcoded Id inside code
public System.Guid RegionId { get; set; }
public System.Guid DistrictId { get; set; }
public Nullable<System.Guid> LocalityId { get; set; }
public string Street { get; set; }
public string House { get; set; }
// a lot of string properties here, nothing intresting
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
internal virtual ICollection<PersonalInformation> PersonalInformations { get; set; }
}
bussines model
public class AddressModel
{
public Guid Id {get;set;}
public Guid RegionId { get; set; }
public Guid DistrictId { get; set; }
public Guid? LocalityId { get; set; }
public string Street { get; set; }
public string House { get; set; }
//one-to-many
public List<PersonalInformationModel> PersonalInformationModels {get;set;}
}
public class PersonalInformationModel
{
public Guid Id {get;set;}
public string FullName{ get; set; }
public Guid? AddressId { get; set; }
public string EMail { get; set; }
private AddressModel _address;
public AddressModel Address
{
get
{
if (AddressId.HasValue && App.CreateCommonService().Addresses.Get(AddressId.Value).Value != null)
return App.CreateCommonService().Addresses.Get(AddressId.Value).Value;
return _address;
}
set { _address = value; }
}
}
和此
的映射说明 internal static class Mapping
{
static Mapping()
{
RegisterMappings();
}
public static TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
{
return Mapper.Map(source, destination);
}
private static void RegisterMappings()
{
try
{
Mapper.AllowNullDestinationValues = true;
Mapper.CreateMap<Address, AddressModel>().ForMember(x=>x.PersonalInformationModels , o=> o.MapFrom(s=>s.PersonalInformations.ToList())).IgnoreAllPropertiesWithAnInaccessibleSetter().ReverseMap();
Mapper.CreateMap<PersonalInformation, PersonalInformationModel>()
.ForMember(x => x.Address, o => o.Ignore()).IgnoreAllPropertiesWithAnInaccessibleSetter().ReverseMap();
Mapper.AssertConfigurationIsValid();
}
catch (Exception ex) when (App.CreateLogService().WriteError(ex))
{
if (Debugger.IsAttached)
Debug.WriteLine(ex);
}
}
}
这就是我如何从我的服务中调用映射
var item = new PersonalInformationModel();
//some input logic and *item* become full
var dbEntity = Mapping.Map(item, new PersonalInformation());
var item = new AddressModel();
//some input logic and *item* become full
var dbEntity = Mapping.Map(item, new Address());
对于这个类,我得到的错误看起来像这样
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==================================================================
AddressModel ->Service (Source member list)
Core.Models.AddressModel -> Data.DB.Address(Source member list)
Unmapped properties:
PersonalInformations
Linq,Core,Entity框架的所有引用在两个项目中相同,.Net框架版本4.6,web.configs中的连接字符串相同(在两个项目中)但MVC工作正常,Api给我未映射的错误。