小编bin*_*bin的帖子

如何在不包含/加载整个集合的情况下获取实体框架模型中列表的计数?

我在 Entity Framework Core 中有一个模型,它是这样的:

public class Anime
{
     public int EpisodeCount { get { return Episodes.Count() } }
     public virtual ICollection<Episode> Episodes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我遇到了 EpisodeCount 的问题0。目前的解决方案是.Include(x => x.Episodes)在我的 EF 查询中运行 a ,但这会加载不需要它的整个剧集集合。这也增加了我的 HTTP 请求时间,从 100 毫秒到 700 毫秒,这并不好。

我不愿意为简单的细节牺牲时间,那么有没有一种解决方案可以让 EF 只查询剧集的 COUNT,而不加载整个集合?

我被建议这样做

var animeList = context.Anime.ToPagedList(1, 20);
animeList.ForEach(x => x.EpisodeCount = x.Episodes.Count());
return Json(animeList);
Run Code Online (Sandbox Code Playgroud)

但这也会0在 EpisodeCount 中返回,因此这不是一个可行的解决方案。

c# entity-framework entity-framework-core asp.net-core

6
推荐指数
1
解决办法
3042
查看次数

Entity Framework Core:如何映射多个相同类型的对象

我有两个类,一个用于User,一个用于Warn上述用户可以接收的类,我想映射它们,但我在这样做时遇到了问题。

using System;
using System.Collections.Generic;
using System.Linq;

namespace web
{
    public class User
    {
        public User()
        {
        }

        public ICollection<MuteWarn> Mutes { get; set; }
        public ICollection<Warn> Warns { get; set; }
        public ICollection<Ban> Bans { get; set; }
        public ICollection<Kick> Kicks { get; set; }
        public int Id { get; set; }
        public ulong DiscordId { get; set; }
        public bool AntiBan { get; set; }
        public ICollection<string> Permissions { get; set; }
        public Level …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core .net-core

3
推荐指数
1
解决办法
2292
查看次数