Attach Files to Objects 将文件附加到对象
This lesson describes how to attach file collections to objects.
本课程描述如何将文件集合附加到对象。
In this lesson you will do the following:
在本课中,你将完成以下内容:
Add the File Attachment Module to the application.
将文件附件模块添加到应用程序中。
Implement new entity classes: Resume to store a Contact’s resume information and PortfolioFileData to save file data collection items.
实现新的实体类:Resume用于存储联系人的简历信息,PortfolioFileData用于保存文件数据集合项。
Note
Before you proceed, take a moment to review the previous lessons:
在继续之前,请花点时间回顾一下之前的课程:
- Implement a Data Model: Basics 实现数据模型:基础
- Configure a One-to-Many Relationship 配置一对多关系
- Implement Property Value Validation 实施属性值验证
Step-by-Step Instructions 分步说明
1.Add the DevExpress.ExpressApp.FileAttachment.Blazor NuGet package to the MySolution.Blazor.Server project and the DevExpress.ExpressApp.FileAttachment.Win NuGet package to the MySolution.Win project. See the following topic for more information on how to install DevExpress NuGet packages: Choose Between Offline and Online DevExpress NuGet Feeds.
将 DevExpress.ExpressApp.FileAttachment.Blazor NuGet 包添加到 MySolution.Blazor.Server 项目中,并将 DevExpress.ExpressApp.FileAttachment.Win NuGet 包添加到 MySolution.Win 项目中。有关如何安装 DevExpress NuGet 包的更多信息,请参阅以下主题:在离线与在线 DevExpress NuGet 源之间进行选择。
2.In the MySolution.Blazor.Server project, open the Startup.cs file and add the File Attachment module to the application builder. Do the same in the Startup.cs file of the MySolution.Win project:
在 MySolution.Blazor.Server 项目中,打开 Startup.cs 文件并将文件附件模块添加到应用程序构建器中。同样地,在 MySolution.Win 项目的 Startup.cs 文件中执行相同操作:
C# (ASP.NET Core Blazor)
public class Startup {
// …
public void ConfigureServices(IServiceCollection services) {
// …
services.AddXaf(Configuration, builder => {
builder.UseApplication();
builder.Modules
// …
.AddFileAttachments();
// …
});
// …
}
}
C# (Windows Forms)
public class ApplicationBuilder : IDesignTimeApplicationFactory {
public static WinApplication BuildApplication(string connectionString) {
var builder = WinApplication.CreateBuilder();
builder.UseApplication();
builder.Modules
//…
.AddFileAttachments();
}
}
If you add the File Attachment Module when you create an XAF application, the Template Kit generates the code that adds the File Attachment Module automatically.
如果在创建XAF应用程序时添加文件附件模块,模板工具包会自动生成添加文件附件模块的代码。
3.In the MySolution.Module\\Business Objects folder, create the Resume class. Replace the generated class declaration with the following code:
在 MySolution.Module\\Business Objects 文件夹中,创建 Resume 类。将生成的类声明替换为以下代码:
C#
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.Collections.ObjectModel;
namespace MySolution.Module.BusinessObjects {
[DefaultClassOptions]
[ImageName("BO_Resume")]
public class Resume : BaseObject {
[Aggregated]
public virtual IListPortfolioFileData> Portfolio { get; set; } = new ObservableCollection();
public virtual Employee Employee { get; set; }
[Aggregated]
public virtual FileData File { get; set; }
}
}
4.Add the Resume property to the Employee class:
将Resume属性添加到Employee类中:
C#
using System.ComponentModel.DataAnnotations.Schema;
namespace MySolution.Module.BusinessObjects {
[DefaultClassOptions]
[ObjectCaptionFormat("{0:FullName}")]
[DefaultProperty(nameof(FullName))]
public class Employee : BaseObject {
// …
public virtual IListResume> Resumes { get; set; } = new ObservableCollection();
}
// …
}
5.Create the PortfolioFileData class. Replace the generated class declaration with the following code:
创建PortfolioFileData类。将生成的类声明替换为以下代码:
C#
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
namespace MySolution.Module.BusinessObjects {
[DefaultClassOptions]
[ImageName("BO_FileAttachment")]
public class PortfolioFileData : BaseObject {
[RuleRequiredField("PortfolioFileDataRule", "Save", "File should be assigned")]
public virtual FileData File { get; set; }
public virtual Resume Resume { get; set; }
public virtual DocumentType DocumentType { get; set; }
public override void OnCreated() {
DocumentType = DocumentType.Unknown;
}
}
public enum DocumentType {
SourceCode = 1,
Tests = 2,
Documentation = 3,
Diagrams = 4,
Screenshots = 5,
Unknown = 6
}
}
6.Add the Required attribute to the Resume property in the PortfolioFileData class.
将PortfolioFileData类中的Resume属性添加Required特性。
C#
// …
namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
[ImageName("BO_FileAttachment")]
public class PortfolioFileData : BaseObject {
//…
[Required]
public virtual Resume Resume { get; set; }
//…
}
The Resume and PortfolioFileData entities are connected with a One-to-Many relationship. For more information on how to create such relationships between entities, refer to the following lesson: Configure a One-to-Many Relationship.
简历和作品集文件数据实体通过一对多关系相连。如需了解如何在实体间创建此类关系,请参考以下课程:配置一对多关系。
In the EF Core-based application, a deletion of a master object does not delete the related objects. In this lesson, we use the Required attribute to configure the associations between classes. This way you can delete the referenced objects with the master object and avoid integrity violation.
在基于EF Core的应用程序中,删除主对象不会删除相关对象。本课程中,我们使用Required特性来配置类之间的关联。通过这种方式,您可以在删除主对象时一并删除被引用的对象,从而避免违反数据完整性。
Alternatively, you can use the Fluent API and specify the OnDelete method for the Portfolio-Resume relationship as described in the following topic: The Fluent API OnDelete Method.
或者,您可以使用 Fluent API 并为 Portfolio-Resume 关系指定 OnDelete 方法,如下文所述:Fluent API 的 OnDelete 方法。
7.Open the MySolution.Module.BusinessObjects\\MySolutionDbContext.cs file and add the properties of Resume and PortfolioFileData types to DbContext:
打开 MySolution.Module.BusinessObjects\\MySolutionDbContext.cs 文件,将 Resume 和 PortfolioFileData 类型的属性添加到 DbContext 中:
C#
public class MySolutionEFCoreDbContext : DbContext {
//…
public DbSet Resumes { get; set; }
public DbSet FileAttachments { get; set; }
}
8.Run the application. Open the Resume List View and create a new Resume object. Fill the Employee field and add a new Portfolio File Data object. In the Portfolio File Data window, select the file that you wish to attach.
运行应用程序。打开 简历 列表视图并新建一个 简历 对象。填写 员工 字段并添加一个新的 作品集文件数据 对象。在 作品集文件数据 窗口中,选择您希望附加的文件。
ASP.NET Core Blazor

Windows Forms

Users can click the file link to download the resume file.
用户可以点击文件链接下载简历文件。
To get a file stored within a PortfolioFileData object in code, use the SaveToStream(Stream) method of its File property.
要在代码中获取存储在 PortfolioFileData 对象中的文件,请使用其 File 属性的 SaveToStream(Stream) 方法。
![P11246 [GESP202409 六级] 小杨和整数拆分-171主机测评](https://www.171host.com/wp-content/uploads/2026/11/20261108100631-6af049a75855c-220x150.png)



