简体   繁体   English

实体框架将属性添加到部分类中,该部分类将从数据库中获取模型或模型列表

[英]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? 如果是这样,那么等效和更好的性能是什么?

EF will ignore the SpokenLanguage property in your case. 在您的情况下,EF将忽略SpokenLanguage属性。

However, you can make EF retrieve the SpokenLanguage using a INNER JOIN by adding a relation in your model between the two tables. 但是,您可以通过在模型中的两个表之间添加关系来使EF使用INNER JOIN检索SpokenLanguage。

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. 您还可以使它懒惰地(按需)检索SpokenLanguage-实际上,它会为您编写的内容提供更好的版本,但是如果您确定要在视图中打印语言标签,则最好使用INNER来检索它。加入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM