asp.net mvc - Understanding virtual and calculated properties in Entity Framework -


i have job model contains number of properties, , set of linked entities called quotes:

public class job {     ....     public virtual icollection<quote> quotes { get; set; } } 

in job class have following calculated property:

public decimal quotesawarded {         {         if (quotes == null || !quotes.any())         {             return 0;         }          var totalunapprovedquotes = quotes.where(x => x.status != "approved");          return 1 - (totalunapprovedquotes.count() / quotes.count());      }     set     {      }  } 

i have 2 questions:

  1. when debug property, quotes null (even though there attached quotes entity). thought using virtual means shouldn't occur? how can ensure whenever model constructed, related quote entities attached?

  2. the reason i'm doing property value stored in database, reduces compute time it's pre-calculated, correct?

follow up:

in cases i'm not using include<quotes> when retrieving job object. i'm using include when need quotesawarded value.

however if don't use include (say db.jobs.find(id)), , quotes null, , quotesawarded value 0. saved database when save job object, i've confused myself here.

for first question, virtual keyword used indication entity framework lazily load this. however, appears have disabled lazy loading need .include(...) it. property reliant on quotes being loaded, return 0.

what doing right, need let entity framework know property computed column. this, need annotate attribute:

[databasegenerated(databasegeneratedoption.computed)] public string quotesawarded {         {         if (quotes == null || !quotes.any())         {             return 0;         }          var totalunapprovedquotes = quotes.where(x => x.status != "approved");          return 1 - (totalunapprovedquotes.count() / quotes.count());     }      private set     {         //make private there's no temptation set     } } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -