学习011-03-13 Map a Persistent Class to a Database View Which Has No Key Field(将持久化类映射到没有键字段的数据库视图)

Map a Persistent Class to a Database View Which Has No Key Field(将持久化类映射到没有键字段的数据库视图)

With XAF, you can build new applications from scratch or maintain existing databases. The How to: Generate XPO Business Classes for Existing Data Tables topic describes how to use the design-time wizard that generates business classes for one or more data tables in the specified database at the same time. Additionally, the existing database can contain views (stored queries), which need to be accessed in an XAF application’s List Views, Analysis, and Reports. If the database view has a key column, you can map a persistent class to it using the same approach as mapping to a regular table.
使用XAF,您可以从头开始构建新应用程序或维护现有数据库。如何:为现有数据表生成XPO业务类主题描述了如何使用设计时向导,同时为指定数据库中的一个或多个数据表生成业务类。此外,现有数据库可以包含视图(存储查询),需要在XAF应用程序的列表视图、分析和报告中访问这些视图。如果数据库视图具有键列,您可以使用与映射到常规表相同的方法将持久类映射到它。

This topic describes how to implement a persistent class mapped to a database view without a key. We used the “Northwind Traders” demo database, shipped with DXperience Suite and installed in %PUBLIC%\Documents\DevExpress Demos 24.1 \Components\Data\nwind.mdb by default, in this topic. Besides tables filled with demo data, this database includes several database views.
本主题介绍如何实现映射到数据库视图的持久类而无需密钥。在本主题中,我们使用了DXperience Suite附带的“NorthWind Traders”演示数据库,并默认安装在%PUBLIC%\Documents\DevExpress Demos 24.1\Components\Data\nwind. mdb中。除了填充了演示数据的表之外,此数据库还包括多个数据库视图。

Note
This approach uses composite keys, which are not supported in the ASP.NET Core Blazor applications, and works only for WinForms and ASP.NET Web Forms applications.
此方法使用ASP.NET Core Blazor应用程序不支持的复合键,仅适用于WinForms和ASP.NET Web Forms应用程序。

1.Create a new XAF solution and follow the steps from the How to: Generate XPO Business Classes for Existing Data Tables topic. Do not forget to modify the connection string that connects your application to the nwind.mbd database.
创建一个新的XAF解决方案并按照如何:为现有数据表生成XPO业务类主题中的步骤进行操作。不要忘记修改将应用程序连接到nwindows. mbd数据库的连接字符串。

2.Open the nwind.mbd database to see what views (queries) it contains. You can use Microsoft Office Access or any other MDB viewer application. In this example, the CustomerReports view is mapped to the CustomerReports persistent class:
打开nwindows. mbd数据库以查看它包含哪些视图(查询)。您可以使用Microsoft Office Access或任何其他MDB查看器应用程序。在此示例中,CustmerReports视图映射到CustmerReports持久类:
在这里插入图片描述

Note
This view contains the ProductName, CompanyName, OrderDate and ProductAmount fields. These names are used when implementing the CustomerReports class.
此视图包含ProductName、CompanyName、OrderDate和ProductAmount字段。这些名称在实现CustmerReports类时使用。

3.Create a new CustomerReports persistent class (you can use the DevExpress 24.1 ORM Persistent Object template). Replace the automatically generated class declaration with the following code:
创建一个新的CustomerReports持久类(您可以使用DevExpress 24.1 ORM持久对象模板)。将自动生成的类声明替换为以下代码:

C#
using System;
using System.ComponentModel;


using DevExpress.ExpressApp.Xpo.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
// ...
[DefaultClassOptions]
public class CustomerReports : XPLiteObject {
    public CustomerReports(Session session) : base(session) { }
    CustomerReportsViewKey fKey;
    [Key, Persistent]
    public CustomerReportsViewKey Key {
        get { return fKey; }
        set { SetPropertyValue(nameof(Key), ref fKey, value); }
    }
    [PersistentAlias("Key.ProductName")]
    public string ProductName { get { return Key.ProductName; } }
    [PersistentAlias("Key.CompanyName")]
    public string CompanyName { get { return Key.CompanyName; } }
    [PersistentAlias("Key.OrderDate")]
    public DateTime OrderDate { get { return Key.OrderDate; } }
    [PersistentAlias("Key.ProductAmount")]
    public string ProductAmount { get { return Key.ProductAmount; } }
}
[TypeConverter(typeof(StructTypeConverter<CustomerReportsViewKey>))]
public struct CustomerReportsViewKey {
    [Persistent("ProductName"), Browsable(false)]
    public string ProductName;
    [Persistent("CompanyName"), Browsable(false)]
    public string CompanyName;
    [Persistent("OrderDate"), Browsable(false)]
    public DateTime OrderDate;
    [Persistent("ProductAmount"), Browsable(false)]
    public string ProductAmount;
}

Each persistent class requires a primary key. The CustomerReports class is an XPLiteObject class descendant, which has no auto-generated key property. So, the Key property representing a composite key (a key formed by combining at least two or more columns) was implemented.
每个持久类都需要一个主键。CustmerReports类是一个XPLiteObject类的后代,它没有自动生成的键属性。因此,实现了表示复合键(由至少两列或多列组合而成的键)的Key属性。

The CustomerReportsViewKey struct defines columns which form a composite key. The struct requires the TypeConverter attribute, which enables ASPxGridView to recognize the key value and to process objects.
CustomerReportsViewKey结构定义构成复合键的列。该结构需要TypeConverter属性,这使ASPxGridView能够识别键值并处理对象。

Additionally, the CustomerReports class exposes properties corresponding to all the view’s columns. You can omit any property, but the composite key should still include all columns.
此外,CustmerReports类公开了与所有视图列对应的属性。您可以省略任何属性,但复合键仍应包含所有列。

Note

  • A database view can already have a key column. In this case, you do not need the composite key, and you should decorate the key property with the KeyAttribute.
    数据库视图可能已经有一个键列。在这种情况下,您不需要复合键,您应该使用KeyAtcade装饰键属性。
  • The number of columns included in the composite key is limited. For instance, Microsoft SQL Server allows a maximum of 16 columns.
    复合键中包含的列数是有限的。例如,MicrosoftSQLServer最多允许16列。

If you do not want your class to have the same name as the database view, you can use a custom name, and decorate the class with the PersistentAttribute:
如果您不希望您的类与数据库视图具有相同的名称,您可以使用自定义名称,并使用PersitentAtcade装饰类:

C#
[DefaultClassOptions, Persistent("CustomerReports")]
public class MyCustomerReports : XPLiteObject {
    // ...
}

4.Run the application. The “Customer Reports” object is available.
运行应用程序。“客户报告”对象可用。
在这里插入图片描述

You can use the CustomerReports class as the Data Type in Reports V2 and Analysis:
您可以在报告V2和分析中使用CustmerReports类作为数据类型:

在这里插入图片描述

As an AI language model, I do not have access to your system logs or configurations to give you a specific solution. However, here are some general troubleshooting steps that you can try to fix the issue of Redis persistent key-value database failing to start: 1. Check if Redis is already running: Run the following command in the terminal to check if Redis is already running: ``` sudo systemctl status redis ``` If Redis is already running, stop it using the following command: ``` sudo systemctl stop redis ``` 2. Check Redis logs: Run the following command to check the Redis logs: ``` tail -f /var/log/redis/redis-server.log ``` This will show the latest logs from Redis. Look for any error messages or warnings that can help you identify the issue. 3. Check Redis configuration: Check the Redis configuration file (/etc/redis/redis.conf) and make sure that the following settings are correct: ``` daemonize yes pidfile /var/run/redis/redis-server.pid logfile /var/log/redis/redis-server.log dir /var/lib/redis ``` 4. Check Redis data directory permissions: Make sure that the Redis data directory (/var/lib/redis) has the correct permissions. Run the following command to set the correct permissions: ``` sudo chown -R redis:redis /var/lib/redis ``` 5. Check Redis port: Make sure that Redis is listening on the correct port (default is 6379). You can check this in the Redis configuration file (/etc/redis/redis.conf). 6. Restart Redis: Restart Redis using the following command: ``` sudo systemctl start redis ``` If the issue persists, try reinstalling Redis or contacting Redis support for further assistance.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值