How to create DevExpress XPO dynamically with multiple primary-keys table?

丶灬走出姿态 提交于 2021-02-09 09:36:52

问题


I try example in this topic https://github.com/DevExpress-Examples/XPO_how-to-create-an-xpclassinfo-descendant-to-dynamically-build-a-persistent-class-structure-e1729

But It only works for single primary key tables. So i searched more Found this: https://github.com/DevExpress-Examples/XPO_how-to-create-persistent-classes-mapped-to-tables-with-a-composite-primary-key-at-runtime-e4606

Buts there's big different and I think its not satisfy because it speaking about composite key( one Key which have many columns) So all I need please an example of creating XPO dynamically from SQL -Server Table: My Table Schema as following

The XPOCollectionSource then binding to grid in server-mode… Thats all I need.

Code I Use

XPServerCollectionSource GetServerModeSourceForTable(IDbConnection connection, string tableName) {  
    XPDictionary dict = new ReflectionDictionary();  
    XPClassInfo classInfo = dict.CreateClass(dict.QueryClassInfo(typeof(LiteDataObject)),  
        tableName);  
    DBTable[] tables = ((ConnectionProviderSql)XpoDefault.GetConnectionProvider(connection,  
        AutoCreateOption.None)).GetStorageTables(tableName);  
    foreach (DBColumn col in tables[0].Columns) {  
        XPMemberInfo member = classInfo.CreateMember(col.Name, DBColumn.GetType(  
            col.ColumnType));  
        if (tables[0].PrimaryKey.Columns.Contains(col.Name))  
            member.AddAttribute(new KeyAttribute());  
    }  
    return new XPServerCollectionSource(new Session(XpoDefault.GetDataLayer(  
        connection, dict, AutoCreateOption.None)), classInfo);  
}  

At a glance. How to use XPServerCollectionSource with Dynamically created XPO object. with two primary keys.


回答1:


I don't know this is what you want but i share my code using XPInstantFeedbackSource.

You can get more information at this url

namespace ...
{
    [Persistent("log.t_log")]
    public class LogXPOModel : XPLiteObject
    {
        [Key, DevExpress.Xpo.DisplayName("id")]
        public long id { get; set; }

        [Key, DevExpress.Xpo.DisplayName("recv_time")]    
        public long recv_time

        ...
}
...
public XPInstantFeedbackSource SqliteXPInstantFeedbackSource
...
SqliteXPInstantFeedbackSource = new XPInstantFeedbackSource();
SqliteXPInstantFeedbackSource.ObjectType = typeof(LogXPOModel);
SqliteXPInstantFeedbackSource.ResolveSession += SqliteXPInstantFeedbackSource_ResolveSession;
SqliteXPInstantFeedbackSource.DismissSession += SqliteXPInstantFeedbackSource_DismissSession;
View.Grid.ItemsSource = SqliteXPInstantFeedbackSource;

Like that my table has two keys and it does not occur any problem.



来源:https://stackoverflow.com/questions/65930328/how-to-create-devexpress-xpo-dynamically-with-multiple-primary-keys-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!