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.
The ModelBuilder is the class which is responsible for building the Model. The ModelBuilder builds the initial model from the entity classes that have DbSet Property in the context class, that we derive from the DbContext class. It then uses the conventions to create primary keys, Foreign keys, relationships etc.
Yes. It's possible. Entity Framework generates Partial Classes.
That means you can create another file that contains another portion of the Partial Class definition (with your additional methods) and everything will work just fine.
An example for the first answer:
if you have an entity named Flower
you can use this partial
class for adding method to it:
namespace Garden //same as namespace of your entity object
{
public partial class Flower
{
public static Flower Get(int id)
{
//
}
}
}
public static class ModelExtended
{
public static void SaveModelToXML(this Model1Container model, string xmlfilePath)
{
///some code
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With