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'
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;
}
}