简体   繁体   English

在实体框架中检索类的数据库计算属性

[英]retrieving database-computed properties of classes within entity framework

I have a good understanding of EF, and generated my database successfully. 我对EF有很好的了解,并成功生成了数据库。 Now, I am struggling with adding dynamic properties to one of the entity classes. 现在,我正在努力为实体类之一添加动态属性。 For example, I have a Post class, and other users can make comments to the posts. 例如,我有一个Post类,其他用户可以对帖子发表评论。 When I list the existing posts, I want to display the number of comments made to corresponding post. 当我列出现有帖子时,我想显示对相应帖子发表的评论数。

One solution might be having a property called CommentCount , and updating the Post by increasing the ( int ) value of the CommentCount property by 1 when a new comment is made. 一种解决方案可能是拥有一个名为CommentCount的属性,并在进行新注释时通过将CommentCount属性的( int )值增加1来更新Post。

The other solution, and I think it is a better solution, is that when retrieving the post from the DB, the number of comments associated with the post can be computed and retrieved at the same time and assigned to CommentCount property of the post instance. 另一个解决方案(我认为是更好的解决方案)是,从数据库检索帖子时,可以同时计算和检索与该帖子关联的注释数量,并将其分配给该帖子实例的CommentCount属性。 However, I do not know how to achieve this with EF. 但是,我不知道如何使用EF实现这一目标。

Which approach is highly recommended? 强烈建议使用哪种方法? Or, is there any other ways of doing this? 或者,还有其他方法可以做到这一点吗? If it is the second one, how can I achieve this with EF? 如果是第二个,我如何使用EF实现呢?

If you calculation is very complex you should try creating a View in your DB and then add it to your Model? 如果计算非常复杂,应该尝试在数据库中创建一个视图,然后将其添加到模型中?

But if your Model have something simple like 但是如果您的模型有一些简单的东西

class Post {
    public int postid { get; set; }
    public virtual ICollection<comment> comment { get; set; }
}

In your controller you can do 在您的控制器中,您可以执行

db.post(x => x.postid == yourid).comments.count() 

to get total of comment 以获得评论总数

or in your view 或您认为

@foreach (var item in Model)
{
    <li>item.postid;</li>
    <li>item.comment.Count();</li>
} 

Or update your class 或更新您的课程

class Post {
    public int postid { get; set; }
    public virtual ICollection<comment> comment { get; set; }
    public int CommentCount
    {
        get 
        { 
            return comment.Count();
        }
    }

}

Just remember bring related data in your query. 只要记住在查询中带入相关数据即可。 In my case POI have properties parish_id, sector_id, city_id and parish have municipality, and municipality have state. 在我的情况下,POI具有属性parish_id, sector_id, city_id和parish具有自治市,而直辖市具有州。

Using this query I can get Poi with all the related data. 使用此查询,我可以获得所有相关数据的Poi。

filter = db.poi
           .Include("parish")
           .Include("sector")
           .Include("city")
           .Include("parish.municipality")
           .Include("parish.municipality.state")
           .Where(x => x.sector_id == SectorID);

1) You should simply consider not putting the property called CommentCount into your model. 1)您应该简单地考虑不要将名为CommentCount的属性放入模型中。 When you develop for example a WPF Windows application, you should consider using MVVM pattern and the CommentCount would belong to your ViewModel class and not to your Model class. 例如,在开发WPF Windows应用程序时,应考虑使用MVVM模式CommentCount将属于ViewModel类而不属于Model类。 There you implement INotifyPropertyChanged and you can use it from your frontend Views . 在那里实现INotifyPropertyChanged并且可以从前端Views中使用它。 Analogically there is MVC pattern for ASP.NET etc. 类似地,有ASP.NET等的MVC模式。

There are other design patterns like Repository pattern . 还有其他设计模式,例如存储库模式 Using this pattern you can create the CommentCount in your repository class and not in your model class. 使用此模式,您可以在存储库类而不是模型类中创建CommentCount This would be similar to your second solution. 这将类似于您的第二个解决方案。

2) I assume from your question that you are using code-first approach: 2)从您的问题中我假设您正在使用代码优先方法:

generated my database successfully 成功生成我的数据库

If you do so and you wish to include CommentCount directly in your Model class, you can do it this by adding partial class file to your project like this: 如果这样做,并且希望直接在Model类中包括CommentCount ,则可以通过将部分类文件添加到项目中来实现此目的,如下所示:

namespace DBModel.Models
{
    public partial class Post
    {
        public int CommentsCount
        {
            get { return this.Comments.Count; }
        }
        ...

But I cannot see why to create extra property in your model just for that. 但是我看不到为什么要为此在模型中创建额外的属性。

On the other hand adding this field as a computed field into your SQL database could make sense and then it would be part of your EF model. 另一方面,将此字段作为计算字段添加到SQL数据库中可能很有意义,然后它将成为EF模型的一部分。

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

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