简体   繁体   English

EF6:参考文献计数

[英]Ef6: count of the references

I have 2 simple objects. 我有2个简单的对象。

public class Person
{
  public int Id { get; set; }
  public string Name { get; set; }
  public List<Message> Messages { get; set; }
  //public int MessageCount { get; set; }
}

Public class Message
{
   public int Id { get; set; }
   public string Text { get; set; }
}

I need to show MessageCounts for each person which is simply the number of messages a person has wrote. 我需要为每个人显示MessageCounts,这只是一个人写的消息数。 How can I do it? 我该怎么做?

I have some ideas but I think they will be very slow, since I will need to return a list of persons and on each I need the count. 我有一些想法,但我认为它们会非常缓慢,因为我需要返回一个人员列表,并且每个人都需要计数。

My Ideas 我的想法

1- in code 1在代码

[NotMapped]
public int MessageCount { get {return Messages.Count()}; private set; }

This is the simplest way that I could imagine, but at the same time it seems to be very slow on a large database since for each person it needs to go and fetch the message count separately which is crazy. 这是我能想到的最简单的方法,但同时在大型数据库上它似乎非常慢,因为对于每个人来说,它都需要单独去获取消息计数,这很疯狂。

2- computed column with a function to return it 2-具有返回值的函数的计算列

It seems like a better plan. 这似乎是一个更好的计划。 right? 对? However, I could not find the whole solution anywhere. 但是,我在任何地方都找不到完整的解决方案。

I know I can decorate my property with [DatabaseGenerated(DatabaseGeneratedOption.Computed)] which will make it read from a computed field, but then how to create a function that returns the value and use that? 我知道我可以用[DatabaseGenerated(DatabaseGeneratedOption.Computed)]装饰属性,这会使它从计算字段中读取,但是如何创建一个返回值并使用该值的函数呢?

I've found something here but he uses the code from the same table which can be done with normal computed fields. 我在这里找到了一些东西但他使用的是同一张表中的代码,可以使用普通的计算字段来完成。 I also this post Calculated column in EF Code First but non of the answers was to my question. 我也在“ EF代码优先”中的“计算后”列中发表了这篇文章但没有答案是我的问题。

-- -

Considering my question, it should be something that you can seen in many applications. 考虑到我的问题,应该在许多应用程序中都能看到。 Isn't there any easy and high performance way to do it? 难道没有任何简便而高性能的方法吗?

Update 更新资料

Thanks to people who commented, I guess the best way is to create 2 types, 1 that corresponds to the real person class and using that for normal CRUD actions and the other which is just a view coming from a join to show lists. 多亏有评论的人,我想最好的方法是创建2种类型,一种对应于真实的人类,并将其用于常规的CRUD动作,另一种只是来自联接显示列表的视图。 Any ideas? 有任何想法吗? :) :)

You will need a relation field between the Message and the user who wrote it, something like this: 您将需要在Message和编写该消息的用户之间建立一个关系字段,如下所示:

Public class Message
{
   public int Id { get; set; }
   public string Text { get; set; }
   public int UserId {get; set; }
}

then when you can count the messages with a simple linq query like this: 那么当您可以使用简单的linq查询来计算消息时,如下所示:

context.Messages.Count(m=> m.UserId == id);

linq is optimized to do this the best as possible, but is the database is very large you will need an approach of the optimization by design, and is better to have a persisted field of the messages count and you can increase it with triggers in the publication or something like that. linq经过了优化,可以做到最好,但是如果数据库很大,您将需要通过设计来进行优化,并且最好保留消息计数的持久字段,并且可以使用触发器中的触发器来增加它出版物之类的东西。

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

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