Domanda

I am using C# Web API and I am using the repository standard to save to the database. Below is a shortened example I am using to update the database. So I use automapper to flatten between user to getuser the region. So when I try to update for example the username using the UpdateUser model. I use automapper to map from updateuser to user. When I try to save it always gives me errors with updating Region. I believe I need the region in the User model because I need it to automap between User to getUser and get the region.

I do not want to update region. I am not understanding how to move forward from this. Let me know if I am going about this ok and what the solution is.

public class User
{
    public int UserID { get; set; }
    public String username { get; set; }
    public virtual Region Region { get; set; }
}

public class GetUser
{
    public int UserID { get; set; }
    public String username { get; set; }
    Public String Region{get; set;}
}

public class UpdateUser
{
    public int UserID { get; set; }
    public String username { get; set; }
    Public String RegionID{get; set;}
}
È stato utile?

Soluzione

From doing some more research. I found that the below automap would ignore region in the automap so the user has the region pulled from the database. Is this the recommended way? Below is what I am using for the automap:

 Mapper.CreateMap<UpdateUser, User>().ForMember(x => x.Region, opt => opt.Ignore());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top