How to: Store the Application Model Differences in the Database 如何:在数据库中存储应用程序模型差异
When you create a new application with the Security System enabled in the Solution Wizard, the end-user settings (model differences) are stored in the database using the ModelDifferenceDbStore storage, by default. This topic describes how to enable this feature in an existing application, along with how to store shared model differences (administrator's settings) in the database.
当您在解决方案向导中启用了安全系统创建新应用程序时,最终用户设置(模型差异)默认使用 ModelDifferenceDbStore 存储存储在数据库中。本主题介绍如何在现有应用程序中启用此功能,以及如何在数据库中存储共享模型差异(管理员设置)。
Note 注意
Mobile applications do not allow you to store model differences in a database, so the approach described in this topic is not supported by the Mobile platform.
移动应用程序不允许在数据库中存储模型差异,因此移动平台不支持本主题中描述的方法。
The System.Security.Principal.WindowsIdentity.GetCurrent().Name value is used as a user identifier (passed to the IModelDifference.UserId property) when the Security System is disabled. So, you can enable ModelDifferenceDbStore for WinForms applications with the disabled Security System using the approach described here. However, it is not recommended that you enable ModelDifferenceDbStore for unsecured ASP.NET applications, because the UserID will be the same for all users in this instance. Shared model differences are supported for both WinForms and ASP.NET when the Security System is disabled.
系统.安全.主体.Windows标识.GetCurrent()。当禁用安全系统时,名称值用作用户标识符(传递给 IModelDifference.UserId 属性)。因此,您可以使用此处描述的方法,使用禁用的安全系统为 WinForms 应用程序启用模型差异DbStore。但是,不建议为不安全的ASP.NET应用程序启用 ModelDifferenceDbStore,因为此实例中的所有用户的用户的 UserID 都相同。禁用安全系统时,WinForms 和ASP.NET都支持共享模型差异。
Edit the WinModule.cs (WinModule.vb) file located in the WinForms module project. In the overridden ModuleBase.Setup method, subscribe to the XafApplication.CreateCustomModelDifferenceStore and XafApplication.CreateCustomUserModelDifferenceStore events. In these event handlers, pass the ModelDifferenceDbStore instance to the e.Store parameter. Pass the ModelDifference type to the ModelDifferenceDbStore constructor. Here, the ModelDifference is a built-in persistent object from the DevExpress.Persistent.BaseImpl namespace (for XPO) or from the DevExpress.Persistent.BaseImpl.EF namespace (for Entity Framework) implementing the IModelDifference interface. Note the constructor's contextId last parameter (used to initialize the IModelDifference.ContextId property). In the WinForms module, set it to "Win" to distinguish model differences created for the same user in different platforms.
编辑位于 WinForms 模块项目中的WinModule.cs (WinModule.vb) 文件。在重写的 ModuleBase.安装程序方法中,订阅 Xaf 应用程序.创建自定义模型差异存储和 Xaf 应用程序.创建自定义用户模型差异存储事件。在这些事件处理程序中,将 ModelDifferenceDbStore 实例传递给 e.Store 参数。将模型差异类型传递给模型差异DbStore构造函数。此处,ModelDifference 是来自 DevExpress.持久性.baseImpl 命名空间(用于 XPO)或来自 DevExpress.持久性.Baseimpl.EF 命名空间(用于实体框架)实现 IModelDifference 接口的内置持久对象。请注意构造函数的上下文 Id 最后一个参数(用于初始化 IModelDifference.ContextId 属性)。在 WinForms 模块中,将其设置为"Win",以区分在不同平台上为同一用户创建的模型差异。
public sealed partial class MySolutionWindowsFormsModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Win"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Win"); e.Handled = true; } //... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
Analogously, edit the WebModule.cs (WebModule.vb) file located in the ASP.NET module project, but set the contextId parameter to "Web" instead of "Win".
类似地,编辑位于ASP.NET模块项目中的WebModule.cs (WebModule.vb) 文件,但将上下文 Id 参数设置为"Web"而不是"Win"。。
public sealed partial class MySolutionAspNetModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Web"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Web"); e.Handled = true; } // ... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
Note 注意
When the CreateCustomModelDifferenceStore event is handled, the shared model differences (administrator settings) are stored in the database. Changes with the Model.xafml file located in the application project are ignored if the database record already exists for the shared model differences. To reload settings from Model.xafml, enable the administrative UI and use the Import Shared Model Difference Action (or delete the "Shared Model Difference" record and restart). If this behavior is inappropriate, do not handle this event. Handle it in the RELEASE project configuration only.
处理"创建自定义模型差异存储"事件时,共享模型差异(管理员设置)将存储在数据库中。如果共享模型差异的数据库记录已存在,则忽略对应用程序项目中的 Model.xafml 文件的更改。要从 Model.xafml 重新加载设置,请启用管理 UI 并使用导入共享模型差异操作(或删除"共享模型差异"记录并重新启动)。如果此行为不恰当,请不要处理此事件。仅在 RELEASE 项目配置中处理它。
If you use the Entity Framework, register the ModelDifference and ModelDifferenceAspect entity types within your DbConext.
如果使用实体框架,请在 DbConext 中注册模型差异和模型差异方面实体类型。
using DevExpress.Persistent.BaseImpl.EF; // ... public class MyDbContext : DbContext { // ... public DbSet<ModelDifference> ModelDifferences { get; set; } public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; } }
I you use XPO, run the Module Designer and add the ModelDifference and ModelDifferenceAspect persistent types to the Exported Types section.
使用 XPO,运行模块设计器,并将模型差异和模型差异方面持久性类型添加到"导出类型"部分。
Ensure that all users have read/write access to ModelDifference and ModelDifferenceAspect types.
确保所有用户都具有对模型差异和模型差异方面类型的读/写访问权限。
public class Updater : ModuleUpdater { public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); PermissionPolicyRole defaultRole = ObjectSpace.FindObject<PermissionPolicyRole>(new BinaryOperator("Name", "Default")); if(defaultRole == null) { defaultRole = ObjectSpace.CreateObject<PermissionPolicyRole>(); defaultRole.Name = "Default"; defaultRole.AddObjectPermission<PermissionPolicyUser>(SecurityOperations.Read, "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddNavigationPermission(@"Application/NavigationItems/Items/Default/Items/MyDetails", SecurityPermissionState.Allow); defaultRole.AddMemberPermission<PermissionPolicyUser>(SecurityOperations.Write, "ChangePasswordOnFirstLogon", "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddMemberPermission<PermissionPolicyUser>(SecurityOperations.Write, "StoredPassword", "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<PermissionPolicyRole>(SecurityOperations.Read, SecurityPermissionState.Deny); defaultRole.AddTypePermissionsRecursively<ModelDifference>(SecurityOperations.ReadWriteAccess, SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<ModelDifferenceAspect>(SecurityOperations.ReadWriteAccess, SecurityPermissionState.Allow); // The 'Create' permission is additionally required if you use the Middle Tier Application Server defaultRole.AddTypePermissionsRecursively<ModelDifference>(SecurityOperations.Create, SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<ModelDifferenceAspect>(SecurityOperations.Create, SecurityPermissionState.Allow); } sampleUser.Roles.Add(defaultRole); // ... ObjectSpace.CommitChanges(); } // ... }
Tip 提示
Refer to the How to: Enable the Administrative UI for managing Users' Model Differences topic to learn how to enable UI elements for managing Model Differences stored in the database.
请参阅"如何:启用管理 UI 以管理用户的模型差异"主题,了解如何启用 UI 元素来管理存储在数据库中的模型差异。
Note 注意
The following combination of features is not supported when used together.
SecuredObjectSpaceProvider or XPObjectSpaceProvider created using the constructor with the threadSafe parameter set to true (this parameter enables the ThreadSafeDataLayer).
Application-wide model differences are stored in the database using the XafApplication.CreateCustomModelDifferenceStore event (you can still store user-specific differences in the database using the XafApplication.CreateCustomUserModelDifferenceStore event).
Custom Persistent Fields declared in the application-level model differences.
当一起使用时,不支持以下功能组合。
使用线程安全参数设置为 true 的构造函数创建的安全对象空间提供程序或 XPObjectSpaceProvider(此参数启用线程安全数据层)。
应用程序范围的模型差异使用 XafApplication 存储在数据库中。Xaf应用程序.创建自定义用户模型差异存储事件)。
在应用程序级模型差异中声明的自定义持久字段。
In this configuration, your application loads information on custom persistent fields from the database and then updates the database schema. However, a thread-safe data layer does not support altering the data model after the database connection is established.
在此配置中,应用程序从数据库中加载自定义持久性字段的信息,然后更新数据库架构。但是,线程安全的数据层不支持在建立数据库连接后更改数据模型。
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端