With entity framework, is it possible to add methods to an object class ? For example, i have a CLIENT mapping and i would like to create a \"getAgeFromBirhDate\" method.
Assuming you have your partial class with an Entity Framework attribute price from the database:
namespace Garden //same as namespace of your entity object
{
public partial class Flower
{
public int price;
public string name;
// Any other code ...
}
}
if you don't want to use another partial class, you can define your own custom class containing the original entity stored as an attribute. You can add then any extra custom attribute and method
namespace Garden //same as namespace of your entity object
{
public class CustomFlower
{
public Flower originalFlowerEntityFramework;
// An extra custom attribute
public int standardPrice;
public CustomFlower(Flower paramOriginalFlowerEntityFramework)
{
this.originalFlowerEntityFramework = paramOriginalFlowerEntityFramework
}
// An extra custom method
public int priceCustomFlowerMethod()
{
if (this.originalFlowerEntityFramework.name == "Rose" )
return this.originalFlowerEntityFramework.price * 3 ;
else
return this.price ;
}
}
}
Then wherever you want to use it, you create your custom class object and store in it the one from the Entity Framework :
//Your Entity Framework class
Flower aFlower = new Flower();
aFlower.price = 10;
aFlower.name = "Rose";
// or any other code ...
// Your custom class
CustomFlower cFlower = new CustomFlower(aFlower);
cFlower.standardPrice = 20;
MessageBox.Show( "Original Price : " + cFlower.originalFlowerEntityFramework.price );
// Will display 10
MessageBox.Show( "Standard price : " + cFlower.standardPrice );
// Will display 20
MessageBox.Show( "Custom Price : " + cFlower.priceCustomFlowerMethod() );
// Will display 30