本文介绍了如何使用多个主键表动态创建DevExpress XPO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试本主题中的示例

然后XPOCollectionSource以服务器模式绑定到网格……这就是我所需要的.

我使用的代码

  XPServerCollectionSource GetServerModeSourceForTable(IDbConnection连接,字符串tableName){XPDictionary dict =新的ReflectionDictionary();XPClassInfo classInfo = dict.CreateClass(dict.QueryClassInfo(typeof(LiteDataObject)),tableName);DBTable []表=(((ConnectionProviderSql)XpoDefault.GetConnectionProvider(connection,AutoCreateOption.None)).GetStorageTables(tableName);foreach(表[0]中的DBColumn col.Columns){XPMemberInfo成员= classInfo.CreateMember(col.Name,DBColumn.GetType(col.ColumnType));如果(tables [0] .PrimaryKey.Columns.Contains(col.Name))member.AddAttribute(new KeyAttribute());}返回新的XPServerCollectionSource(new Session(XpoDefault.GetDataLayer(连接,字典,AutoCreateOption.None)),classInfo);} 

一目了然.如何将 XPServerCollectionSource 与动态创建的XPO对象一起使用.有两个主键.

解决方案

要通过键或条件搜索对象,请使用以下内容:

I try example in this topichttps://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.

解决方案

https://github.com/DevExpress-Examples/XPO_how-to-create-persistent-classes-mapped-to-tables-with-a-composite-primary-key-at-runtime-e4606/blob/19.2.7%2B/CS/XpoConsoleApplication/XPComplexCustomMemberInfo.cs

This is a class must used. who need more details about that. I can help him for free.

Implementation can be like:

[NonPersistent]
public class XPDynamicObject  : XPLiteObject
{
    public XPDynamicObject(Session session) : base(session) {}
    public XPDynamicObject(Session session, XPClassInfo classInfo) : base(session, classInfo) { }
}


       Button_Click or any event :

       XPDynamicObject.AutoSaveOnEndEdit = false;

        ReflectionDictionary dic = new ReflectionDictionary();
        var classInfo = dic.CreateClass(dic.GetClassInfo(typeof(XPDynamicObject)), "general.users");

        // WE MUST get schema from database .... via ConnectionProviderSql this is only way
        var provider = XpoDefault.GetConnectionProvider(MSSqlConnectionProvider.GetConnectionString("(local)", "testdb"), AutoCreateOption.None) as ConnectionProviderSql;


        // Composite Key - this is only way to add composite key dynamically
        XPComplexCustomMemberInfo user_key = new
        XPComplexCustomMemberInfo(classInfo, "user_key", typeof(object), new KeyAttribute(), new PersistentAttribute(), new BrowsableAttribute(false));
        user_key.AddSubMember("user_brn", typeof(int), new PersistentAttribute("user_brn"));
        user_key.AddSubMember("user_num", typeof(int), new PersistentAttribute("user_num"));

        var user_name = classInfo.CreateMember("user_name", typeof(string));
        user_name.AddAttribute(new PersistentAttribute("user_name"));


        dal = new SimpleDataLayer(dic, provider);

        XPServerCollectionSource xpServerCollectionSource = new XPServerCollectionSource(session, classInfo); // XPServerCollectionSource Only Editable server-mode datasource via ALlowNew, AllowEdit, AllowRemove properties

GridControl.DataSource = xpServerCollectionSource ;

Full Cheat Sheet (Design-Time and Run-Time XPOs)

For Searching for Object by Key or Condition use following:

这篇关于如何使用多个主键表动态创建DevExpress XPO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:28