0

I have a EF model class Comment with a navigation property ScoreVotes referencing ScoreVote which has member int Score. In Comment I have the following method Score():

    public int Score()
    {
        var votes = this.ScoreVotes;

        if (votes == null)
            return 0;
        else
        {
            int score = 0;

            foreach (var scoreVote in votes)
            {
                score += scoreVote.Score;
            }

            return score;
        }
    }

I have the following questions:

  1. How can I tell EF to bring over the Comment's ScoreVotes up-front if I know that I will need to calculate the score? (Should I use Include?)
  2. If I made Score a property whose getter method matched the current method, would Score be recorded in the database? Would it always reflect the last calculated score?
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

1 Answers1

0

Create the column Score in the Table Comment. And just update the Score column when you add/update/delete the ScoreVotes related to the Comment.

There's also the use of calculated fields.

Here are more options. https://stackoverflow.com/a/12817314/181766

Community
  • 1
  • 1
jjchiw
  • 4,375
  • 1
  • 29
  • 30