简体   繁体   English

在 DbSet 上使用包含<tentity>核心</tentity>

[英]using include on DbSet<TEntity> ef core

I have several classes that all have IEnumerable of type File.我有几个类都具有文件类型的 IEnumerable。 but I am struggling to use include with DbSet但我正在努力将包含与 DbSet 一起使用

 public class FileRepository<T> : IFileRepository<T> where T : class
    {
        private readonly DataContext context;
        private readonly DbSet<T> table = null;

        public FileRepository(DataContext ctx)
        {
            context = ctx;
            table = context.Set<T>();
        }

        public async Task<object> GetByIdAsync(long id)
        {
            if (id > 0)
            {
                return (await table
                  .Include(x => x.Files)
                  .FirstAsync(x => x.Id == id)
                  .ConfigureAwait(false))
                  .Files.ToList();
            }
            else
            {
                return new List<File>();
            }
        }

         
    }

Severity Code Description Project File Line Suppression State Error CS1061 'T' does not contain a definition for 'Files' and no accessible extension method 'Files' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)严重性代码 描述 项目文件行抑制 State 错误 CS1061“T”不包含“文件”的定义,并且找不到接受类型“T”的第一个参数的可访问扩展方法“文件”(您是否缺少 using 指令或装配参考?)

EDIT You're attempting to write code for a specific service is a generic.编辑您尝试为特定服务编写代码是通用的。 You need to add constraints on T.您需要在 T 上添加约束。

eg T is Generic so there is no way to know there is a property.例如 T 是通用的,所以没有办法知道有一个属性。 Id or Files on there, write your logic in a layer up. Id 或 Files 在那里,将您的逻辑写在一个层中。

public class EmployeeFiles<Employee> : IFileRepository<Employee>
{
    //... Write your logic here instead so the logic is specific to the Entity.
}

Alternatively you can have all Entity which you want to implement the file api implement an inteface或者,您可以让所有要实现文件 api 的实体实现接口

 public interface IHasFileProperties
 {
      public int Id {get; set;}
      public ICollection<File> Files { get; set; }
 }

and have the file repo:并有文件回购:

  IFileRepository<T> where T : IHasFileProperties

Additionally from the problem you presented GetById should return T and not a list of Files, if you want to get the list of files you should make an extension on your existing repo.此外,从您提出的问题 GetById 应该返回 T 而不是文件列表,如果您想获取文件列表,您应该在现有存储库上进行扩展。

Side-Note: ASP.Net-core got rid of the legacy synchronization context so you don't need to call .ConfigureAwait(false)) anymore unless you're using a library targeting the older .net 4.5 or apps with UI, see this blog by Stephen Cleary for the full detail, for a brief overview read the comments left by @IvanStoev旁注: ASP.Net-core 摆脱了旧的同步上下文,因此您不再需要调用.ConfigureAwait(false)) ,除非您使用的是针对较旧的 .net 4.5 或具有 UI 的应用程序的库,请参阅此由 Stephen Cleary 撰写的博客了解详细信息,有关简要概述,请阅读@IvanStoev 留下的评论

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

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