How to get class Entity Framework inherits base class

I have several classes that inherit from an abstract base class that contains some general utility code. I want to go to EF to access the data, but I still would like the objects to inherit the common code in the base class. EF classes already inherit from EntityObject, so I cannot inherit my base class. What is the right way to handle this? My environment is Net 3.5 / C #

+3
source share
2 answers

Sorry if you saw my previous post - I missed something important in your question.

You can use partial classes. Make your incomplete class an internal field with an instance of the base class that you want, and implement all the methods and properties (if they are not already implemented in essence). If you are trying, then to allow the use of your entity class, you can use public static implicit operatorin your base class (or in the class of partial entities) to allow conversion without hassle.

If you have an abstract base class MyBaseClass, you can do something like:

public partial class MyEntityClass
{
    private MyBaseClass _baseClass;

    private MyBaseClass BaseClass
    {
        get
        {
            if (_baseClass == null)
            {
                _baseClass = new MyBaseClass();
            }
            return _baseClass;
        }
    }

    public string BaseClassString
    {
        get
        {
            return BaseClass.BaseClassString;
        }
        set
        {
            BaseClass.BaseClassString = value;
        }
    }

    // etc.

    public static implicit operator MyBaseClass(MyEntityClass e)
    {
        return new MyBaseClass() {
            Property1 = e.Property1,
            Property2 = e.Property2 // etc.
        };
    }

    public static implicit operator MyEntityClass(MyBaseClass b)
    {
        return new MyEntityClass() {
            Property1 = b.Property1,
            Property2 = b.Property2 // etc.
        };
    }
}
+5
source

If you are still using Visual Studio 2008, I'm not sure if you can do this (although someone, please feel free to correct me).

VS2010, T4 ( .net 3.5). , EntityObject, t4, , .

VS2008, , , , CS ( , ), , .

+2

Source: https://habr.com/ru/post/1765351/


All Articles