devexpressxafxpo

Devexpress xaf many to many relationship oid key Name Change


I want to set many to many relationship oid key name. In many to many relationship Oid is created automatically but on database side I want to change oid name to custom name. For Example;

If I try to create Person and Task many to many relation. Third table attributes in below;

KomutTanim (FK to Makine) Makine (FK to KomutTanim) OID (PK, guid)** (I want to set this key name??)**

Tell me how can I do. I added sample code in below

    [Association("Relation.KomutListesi_Makine",typeof(KomutTanim),UseAssociationNameAsIntermediateTableName = true),XafDisplayName("Makine Komutları")]
public XPCollection<KomutTanim> Komutlar
{
get
{
return GetCollection<KomutTanim>(nameof(Komutlar));
}
}

[Association("Relation.KomutListesi_Makine", typeof(Makine), UseAssociationNameAsIntermediateTableName = true), XafDisplayName("Makineler")]
public XPCollection<Makine> MasterId
{
get
{
return GetCollection<Makine>(nameof(MasterId));
}
}

Solution

  • You can customize XPO metadata or manually create a persistent class for your intermediate table. These approaches are illustrated in the How to implement a many-to-many relationship with an intermediate table ticket.

    The solution with customizing XPO metadata uses XAF APIs to access an XPClassInfo instance via the XPDictionary property. You can access XPDictionary using only XPO methods as illustrated at How to get an XPClassInfo instance. Also, you can manually create a ReflectionDictionary instance (ReflectionDictionary is an XPDictionary descendant) as described in the How to create persistent metadata on the fly and load data from an arbitrary table article.

    XPDictionary dictionary = new ReflectionDictionary();
    XPClassInfo intermediateClassInfo = dictionary.GetClassInfo(typeof(KomutTanim)).FindMember(nameof(KomutTanim.MasterId)).IntermediateClass;
    intermediateClassInfo.FindMember("Oid").AddAttribute(new PersistentAttribute("MyName"));
    
    string conn = "My connection string";
    
    IDataStore store = XpoDefault.GetConnectionProvider(conn, AutoCreateOption.SchemaAlreadyExists);
    
    IDataLayer dl = new SimpleDataLayer(dictionary, store);
    
    XpoDefault.DataLayer = dl;