Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

creating and selecting custom metaclasses without backreferences

$
0
0

I need to create a custom metaclass that is associated with CustomerContact, although i dont want to extended the Contact metaclass with a backreference, because i presume then every load of a contact will fill the back reference as well? Anyway, my issue is I'm having trouble setting each custom metaclass's reference to the contact...

here is how I'm extending my metaclass with contact reference: 

statementMetaClass = DataContext.Current.GetMetaClass(BF.MetaClass.Statement); // simplified, may throw an exception
if (statementMetaClass == null)
    {
        statementMetaClass = DataContext.Current.MetaModel.CreateMetaClass(BF.MetaClassTableName.Statement,
            BF.MetaClassTableName.StatementFriendlyName);
        using (MetaClassManagerEditScope scope = DataContext.Current.MetaModel.BeginEdit())
        {
            // Create reference - "Contact" is EPiServer's string literal for CustomerContact
            statementMetaClass.CreateReference(metaClassContact, name: "Contact", friendlyName: "Contact", true);
            // Save Changes
            scope.SaveChanges();
        }
    }

and then I'm trying to insert the foreign key value like this:

//customMetaClass.Properties["ContactId"].Value = (Guid)Customer.PrimaryKeyId.Value; // simplified, field needs to exist to be set this way
//EDIT: actually this is the code being called because the field doesn't exist
entObj.Properties.Add(key, value);

and then later trying to select all of the custom metaclasses associated with the contact like this:

FilterElement filter = FilterElement.EqualElement("ContactId",(Guid)theContact.PrimaryKeyId.Value);
var reserves = BusinessManager.List(BF.MetaClass.Statement, new[] { filter });

but the select isn't working correctly, because i cant see the 'contactId' being set correctly when i create/insert a new instance of the custom metaclass.

Can anyone see any obvious mistakes I'm making, or is there a better approach?


Viewing all articles
Browse latest Browse all 9076

Trending Articles