Search code examples
c#asp.net-mvcentity-frameworkpartial-classes

Entity Framework add property to partial class that will get model or list of models from database


I have added a property to a partial class of a model. This property will retrieve a model from database according to property value.

Example:

class movie
{
    int language;
}

partial movie 
{
    public Language SpokenLanguage
    {
       get
       {
          var currLang = db.Languages.Where(ml => ml.ID == this.language).FirstOrDefault();
          return currLang;
       }
   }
}

Is this approach will affect application performance when I retrieve a list of movies?

If so what is the equivalent and better performance?


Solution

  • EF will ignore the SpokenLanguage property in your case.

    However, you can make EF retrieve the SpokenLanguage using a INNER JOIN by adding a relation in your model between the two tables.

    You can also make it retrieve the SpokenLanguage lazily (on demand)-it will actually make a better version on what you wrote, but if you are sure you want to print the language label in your view, it's better to retrieve it using a INNER JOIN.