I am using Database First approach and I have created model from a database. Now I have a datagrid view in my Winforms app, that is bound to a binding source. And all that works fine (an appropriate data is shown in datagrid view). Now the problem is, how to add a computed property that consists of two values (already found in db) ? For an example:
Lets say that I have a table user (id, username, first_name, last_name, user_type) but I want to have different columns in my bound datagrid view, and I want to have these columns:
username, full name, type
where "full name"
is what would I get with first_name + " " + last_name
.
I guess I can't just modify a model class manually like this:
public string FullName
{
get
{
return FirstName + " " + LastName;
}
protected set {}
}
because this class is generated automatically , and my code will be deleted each time a generate models from an existing database (when I make some change), so this is not the real option...
Actually, I solved this by using partial class functionality: I have created another file that contains another portion of my User model class (with my additional properties) and everything went just fine.
namespace User.Model
{
public partial class User
{
public string FullName
{
get
{
return (this.firstName + " " + this.lastName;
}
protected set { }
}
}
}
Now when I generate model classes, this new file is not affected by EF. Also this new field is correctly shown by datagrid view...