5

我有两个类,一个是由实体框架生成的,另一个是我到处使用的类。

我的课 :

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

EF类:

public class PERSON
{
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
}

当源为PERSONto时我找到了解决方案Person我没有找到 to 的解决方案PersonPERSON属性为大写和下划线分隔符)。

The solution for PERSON to Person :

Mapper.Initialize(x => x.AddProfile<Profile1>());
var res = Mapper.Map<PERSON, Person>(person);

public class UpperUnderscoreNamingConvention : INamingConvention
{
    private readonly Regex _splittingExpression = new Regex(@"[p{Lu}0-9]+(?=_?)");
    public Regex SplittingExpression
    {
        get { return _splittingExpression; }
    }

    public string SeparatorCharacter
    {
        get { return "_"; }
    }
}

public class Profile1 : Profile
{
    protected override void Configure()
    {
        SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
        DestinationMemberNamingConvention = new PascalCaseNamingConvention();
        CreateMap<PERSON, Person>();
    }
}
4

1 回答 1

1

这适用于 AutoMapper 7.0.1 版:

using AutoMapper;
using System.Text.RegularExpressions;


namespace Data.Service.Mapping
{
    public class UpperUnderscoreNamingConvention: INamingConvention
    {
        public Regex SplittingExpression { get; } = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=_?)");

        public string SeparatorCharacter => "_";

        public string ReplaceValue(Match match) => match.Value.ToUpper();
    }
}
于 2018-10-25T16:32:27.907 回答