@manhnguyenv

Welcome to my blog!

Auto Mapper

April 22, 2019 21:53

Auto Mapper (edit)

Integrate with ASP.NET MVC 5.2.3

Auto Mapper Version: 6.2.2.0

https://www.codeproject.com/Articles/61629/AutoMapper

https://www.c-sharpcorner.com/blogs/using-automapper-in-c-sharp

https://www.c-sharpcorner.com/article/getting-started-with-automapper/

Global.asax.cs


public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapperConfiguration
.Initialize();
}
}

AutoMapperConfiguration.cs

    public class AutoMapperConfiguration
    {
        public static void Initialize()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<UserAccount>() // User    -> Account
                    .ReverseMap();             // Account -> User
            });
        }
    }

Using the Code

Account objAccount = Mapper.Map<Account, User>(objAccount);

Account.cs

    public class User
    {
        public int Id { getset; }
        public string UserName { getset; }
    }

User.cs

    public class User
    {
        public int Id { getset; }
        public string UserName { getset; }        
public
 string Password { getset; } }

Categories

Recent posts