简体   繁体   English

ASP.NET 内核 - 如何解决“ICollection”<approvalattachment> ' 不包含 'FilePath' 的定义并且没有可访问的扩展方法</approvalattachment>

[英]ASP.NET Core - How to resolve 'ICollection<ApprovalAttachment>' does not contain a definition for 'FilePath' and no accessible extension method

In ASP.NET Core-6 Web API, I have these models:在 ASP.NET Core-6 Web API 中,我有这些模型:

public class LeaveApplication
{
    public Guid Id { get; set; }
    public Guid EmployeeId { get; set; }
    public string ReferenceNumber { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public virtual Employee Employee { get; set; }
    public virtual LeaveApproval LeaveApproval { get; set; }
}

public class LeaveApproval
{
    public Guid Id { get; set; }
    public bool? IsApproved { get; set; }
    public DateTime? ApprovedDate { get; set; } = null;
    public virtual ICollection<LeaveApprovalAttachment> LeaveApprovalAttachments { get; set; }
}

public class LeaveApprovalAttachment
{
    public Guid Id { get; set; }
    public Guid LeaveApprovalId { get; set; }
    public string FileType { get; set; }
    public string FileName { get; set; }
    public string FilePath { get; set; }
    public long? FileSize { get; set; }
    public virtual LeaveApproval LeaveApproval { get; set; }
}

A LeaveApplication has one LeaveApproval, while a LeaveApproval can have many LeaveApprovalAttachments.一个 LeaveApplication 有一个 LeaveApproval,而一个 LeaveApproval 可以有多个 LeaveApprovalAttachments。

Based on parameter id, I want to get a filepath of a LeaveApprovalAttachment and download the document.根据参数 id,我想获取 LeaveApprovalAttachment 的文件路径并下载文档。 So, I have this code:所以,我有这个代码:

public async Task<LeaveApplication> GetLeaveDocumentByIdAsync(Guid id)
{
    var leave = await _dbContext.LeaveApplications
                    .Where(m => (bool)m.IsApproved)
                    .Include(x => x.LeaveApproval)
                    .ThenInclude(x => x.LeaveApprovalAttachments)
                    .FirstOrDefaultAsync(x => x.LeaveApproval.LeaveApprovalAttachments.Any(x => x.Id == id));
    var path = Path.Combine(Directory.GetCurrentDirectory(), leave.LeaveApproval.LeaveApprovalAttachments.FilePath);
    var memory = new MemoryStream();
    using (var stream = new FileStream(path, FileMode.Open))
    {
       await stream.CopyToAsync(memory);
    }
    memory.position = 0;
    var contentType = "APPLICATION/octet-stream";
    var fileName = Path.GetFileName(path);
    return File(memory, contentType, fileName);
}

But I got this error:但我得到了这个错误:

Error CS1061 'ICollection' does not contain a definition for 'FilePath' and no accessible extension method 'FilePath' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“ICollection”不包含“FilePath”的定义,并且找不到接受“ICollection”类型的第一个参数的可访问扩展方法“FilePath”(您是否缺少 using 指令或程序集引用?)

and it highlights FilePath in leave.LeaveApproval.LeaveApprovalAttachments.FilePath它在leave.LeaveApproval.LeaveApprovalAttachments.FilePath中突出显示FilePath

How do I resolve this?我该如何解决这个问题?

Thanks谢谢

From here:从这里:

public virtual ICollection<LeaveApprovalAttachment> LeaveApprovalAttachments { get; set; }

LeaveApprovalAttachments property is a collection. LeaveApprovalAttachments属性是一个集合。 You can't direct access via leave.LeaveApproval.LeaveApprovalAttachments.FilePath .您不能通过leave.LeaveApproval.LeaveApprovalAttachments.FilePath直接访问。

Instead, you need to provide the index to LeaveApprovalAttachments as:相反,您需要将索引提供给LeaveApprovalAttachments ,如下所示:

leave.LeaveApproval.LeaveApprovalAttachments[0].FilePath

Or to get the first item of LeaveApprovalAttachments :或者获取LeaveApprovalAttachments的第一项:

using System.Linq;

var leaveApprovalAttachment = leave.LeaveApproval.LeaveApprovalAttachments.FirstOrDefault();
if (leaveApprovalAttachment != null)
{
    var path = Path.Combine(Directory.GetCurrentDirectory(), leaveApprovalAttachment.FilePath);

    ...
}

You're getting that error because LeaveApprovalAttachments is of type ICollection<LeaveApprovalAttachment> and not LeaveApprovalAttachment .您收到该错误是因为LeaveApprovalAttachments的类型是ICollection<LeaveApprovalAttachment>而不是LeaveApprovalAttachment You can see from the documentation of ICollection<T> that there is no FilePath property.您可以从ICollection<T>的文档中看到没有FilePath属性。 You also can't just access a property of the element type, which is LeaveApprovalAttachment in this case, from the ICollection<T> because you have multiple LeaveApprovalAttachment s.您也不能只从ICollection<T>访问元素类型的属性,在本例中为LeaveApprovalAttachment ,因为您有多个LeaveApprovalAttachment How can it know which one you want to access?它怎么知道你想访问哪一个?

Looking at your code, it seems like you only need a single LeaveApprovalAttachment , so the best way to resolve it in this particular case is probably just to select the one LeaveApprovalAttachment that you need, rather than the entire LeaveApplication .查看您的代码,您似乎只需要一个LeaveApprovalAttachment ,因此在这种特殊情况下解决它的最佳方法可能只是 select 您需要的一个LeaveApprovalAttachment ,而不是整个LeaveApplication

Maybe something like this...也许像这样的东西......

var leaveAttachment = _dbContext
    .LeaveApplications
    .Include(la => la.LeaveApproval)
    .ThenInclude(la => la.LeaveApprovalAttachments)
    .Where(la => (bool)la.IsApproved)
    .SelectMany(la => la.LeaveApproval.LeaveApprovalAttachments)
    .FirstOrDefault(laa => laa.Id == id);    

Note that you're querying on LeaveApplication.IsApproved which doesn't seem to exist on your model, but I tried to replicate your query as much as possible.请注意,您正在查询LeaveApplication.IsApproved ,它似乎在您的 model 上不存在,但我尝试尽可能多地复制您的查询。

暂无
暂无

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

相关问题 “IServiceCollection”不包含“AddAWSService”的定义,并且在asp.net core 2.2 中没有可访问的扩展方法“AddAWSService”错误? - 'IServiceCollection' does not contain a definition for 'AddAWSService' and no accessible extension method 'AddAWSService' error in asp.net core 2.2? ASP.NET Core:IConfigurationBuilder 不包含 AddAzureKeyVault 的定义 - ASP.NET Core: IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault EF Core ICollection不包含“包含”的定义 - EF Core ICollection does not contain a definition for 'Include' 不包含 &#39;AddComponent&#39; 的定义,并且在 Unity 中没有可访问的扩展方法 &#39;AddComponent&#39; 错误 - Does not contain a definition for 'AddComponent' and no accessible extension method 'AddComponent' error in Unity 'Avro解串器<genericrecord> ' 不包含 'AsSyncOverAsync' 的定义并且没有可访问的扩展方法</genericrecord> - 'AvroDeserializer<GenericRecord>' does not contain a definition for 'AsSyncOverAsync' and no accessible extension method “BitmapImage”不包含“SetSource”的定义,也没有可访问的扩展方法“SetSource” - 'BitmapImage' does not contain a definition for 'SetSource' and no accessible extension method 'SetSource' MVC 5 ASP.NET IEnumerable <Item> 不包含“检查”的定义,也不包含接受类型为第一个参数的扩展方法“检查” - MVC 5 ASP.NET IEnumerable<Item> does not contain a definition for “Inspection” and no extension method “inspection” accepting a first argument of type 用户经理<XYZ> &#39; 不包含 &#39;CreateIdentityAsync&#39; 的定义并且没有可访问的扩展方法 - UserManager<XYZ>' does not contain a definition for 'CreateIdentityAsync' and no accessible extension method C# - string[] 不包含“SkipLast”的定义并且没有可访问的扩展方法 - C# - string[] does not contain a definition for 'SkipLast' and no accessible extension method Objekt 不包含“SerializeObject”的定义,也没有可访问的扩展方法 - Objekt does not contain a definition for 'SerializeObject' and no accessible extension method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM