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.
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:
GridControl.DataSource = xpServerCollectionSource ;
Full Cheat Sheet (Design-Time and Run-Time XPOs)
For Searching for Object by Key or Condition use following:
Edit: 2021-05-24
For those who asks about Associations with composite keys. Its really nightmare. By default Xpo doesn't provide that. instead you can use a Session.GetObjectByKey() in child-class and a new XPCollection in parent class. But actually its something little slowly that native way with an artificial key.
Here's a code for associations with composite keys. (Note that you need to convert it dynamically like above pictures. You maybe need to create custom XPMemberInfo and override GetValue() to return Session.GetObjectByKey() and create new XPCollection. (really bad stuff to do and worst ever and non-supported ever by Xpo. So Composite-Keys technically are bad way except if you need create Single Table bind it to grid. EX:- SalesOrderDetail table which doesn't have more child tables)
A Dynamic run-time version from above model:
Finally, IMPORTANT !