Add methods to a model using entity framework

前端 未结 4 759
暖寄归人
暖寄归人 2021-02-13 01:52

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.

相关标签:
4条回答
  • 2021-02-13 01:59

    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.

    0 讨论(0)
  • 2021-02-13 02:04

    An example for 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)
            { 
                //
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-13 02:14

    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
    
    0 讨论(0)
  • 2021-02-13 02:26
    public static class ModelExtended
    {
        public static void SaveModelToXML(this Model1Container model, string xmlfilePath)
        {
            ///some code
        }
    }
    
    0 讨论(0)
提交回复
热议问题