2

I have a Baseclass that i will need to inherit for my models

like

public class Employee : BaseModel
{

}

In this BaseModel I have a couple of properties and some default implementations that needs to be available for every application model created.

When i plan to migrate to entity framework, how do i have the model that is generated with EF have my methods and properties also from the BaseModel.

However, i have removed the EntityObject from the generated class and commented out its related implementation in each property like

   //ReportPropertyChanging("id");
                    //_id = StructuralObject.SetValidValue(value);
                    //ReportPropertyChanged("id");

Kindly suggest if this is a right way or is there a correct way to deal with this kind of scenario.

1

1 Answer 1

1

The nitpicking OO purist speaking: having one base class for all classes in your domain is an anti pattern. Classes that obey OO principles (like single responsibility, Liskow) can impossibly all have the same base class. Without knowing your base class, I bet that the common properties have to do with common database fields or some entity framework implementation, so a base class also breaks persistence ignorance.

The entity-framework guru (who???) speaking: you better don't use a base class when working database-first. Or: when you really want a base class: work code-first and ignore the base class in the mappings, i.e. do not map the inheritance. Note that the DbContext API is the preferred EF API (but you can also use it db-first).

The pragmatist speaking: use an interface if this is feasible. It's a nuisance that each class has to implement it again and again, but you won't break OO principles. The principles are there for a reason. Sooner or later, breaking them will handicap (but probably not inhibit) you.

Sign up to request clarification or add additional context in comments.

2 Comments

Kindly note that I have common property called as id and have common functions that will enable changes in the entity to be recorded etc. So i hope that will not be against any pattern. Please suggest if possible in Entity Framework
Maybe you should show more details for us to be able to evaluate that. If you use DbContext (POCO) API your objects won't be involved in change tracking. EF will handle that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.