How to convert entity object to model class object

asp.net-mvc c# entity-framework

Question

I am new to entity framework. I want to know whether there is a way to convert entity type to the model class

public UserModel GetUserDetailsById(int userId)
{
    using (var db = new klpm04222014Entities())
    {
         return db.klpm_user.Single(x => x.UserID == userId);
    }
}

but it is giving error stating that

Cannot implicitly convert type 'klpm_user' to 'UserModel'   
1
0
4/22/2015 11:53:48 AM

Popular Answer

public UserModel GetUserDetailsById(int userId)
{
    using (var db = new klpm04222014Entities())
    {                                
        var user = db.klpm_user.SingleOrDefault(x => x.UserID == userId);
        if(user != null) 
        {
            // map the properties of your entity to the UserModel
            return new UserModel {
                UserID = user.UserID,
                ...
            };
        }
        return null;
    }
}
1
4/22/2015 12:08:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow