CRM 2016 FakeXrmEasy N:N relationships
Asked Answered
V

1

5

I'm trying to use FakeXrmEasy to perform some unit tests for CRM Online (2016) and I'm having problems setting up one of my tests with an N:N relationship

The following code sets up a Faked Context with 2 entities in it and initializes a Faked Organization Service:

var entity1 = new New_entityOne();
var entity2 = new New_entityTwo();

var context = new XrmFakedContext();
context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(New_entityOne));
context.Initialize(new List<Entity>()
{
    entity1,
    entity2
});

var service = context.GetFakedOrganizationService();

I then try to create an N:N relationship between these entities:

var join = new AssociateRequest
{
    Relationship = new Relationship
    {
        SchemaName = "new_entityOne_new_entityTwo",
        PrimaryEntityRole = EntityRole.Referenced
    },
    Target = entity1.ToEntityReference(),
    RelatedEntities = new EntityReferenceCollection
    {
        entity2.ToEntityReference()
    }
};

service.Execute(join);

When I execute this Request, I'm expecting a N:N-join record to be produced in my mock data, between entity1 and entity2

Instead I'm getting an error like this:

An exception of type 'System.Exception' occurred in FakeXrmEasy.dll but was not handled in user code

Additional information: Relationship new_entityOne_new_entityTwo does not exist in the metadata cache

Has anyone else tried using this unit framework in this way? Up until this point I have been getting really good results using it.

obviously, these are not my actual entity and relationship names

Vincentvincenta answered 19/5, 2016 at 8:27 Comment(0)
B
3

Please try adding a fake relationship as shown here

This is because for N:1 there's no intersect table, joins are performed via an EntityReference and that's it, but for many to many, as there is an intersect table, we need to tell the framework how to deal with this scenario for now.

There was also an update where it is no longer mandatory to use ProxyTypesAssembly, as long as you use early bound types, the proxy types assembly will be "guessed" from your types.

So you could remove this

context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(New_entityOne));

I'll need to update the documentation in the web site... whenever I have a chance :)

Edit

Web site updated: http://dynamicsvalue.com/get-started/nn-relationships

Brodeur answered 19/5, 2016 at 11:57 Comment(8)
Thanks a lot Jordi, I've been finding your examples really helpful, I just couldn't find this one. I'll also remove that deprecated lineVincentvincenta
@Vincentvincenta You´re very welcome. Feel free to email me if you have any other questions, it might be quicker as I didn't realize there was this question here until you mentioned me directly :)Brodeur
@Vincentvincenta Just deployed a new version of the web site with a new section for N:N relationships. If you find anything else could be there, please let me know :)Brodeur
@great, thanks Jordi, that's really helpful. I actually just cloned your repo. I was looking to see if I could implement FakeRetrieveAttributeRequest but not really sure where to begin yet. I'm trying to figure out how to retrieve optionset labelsVincentvincenta
@Vincentvincenta Amazing stuff! :) Maybe better to discuss it as a new issue on Git? I was thinking on doing it via reflection, as long as you use proxy types, the labels could be retrieved from the associated enum. That's one option.Brodeur
@Vincentvincenta , I've raised an issue on git to keep track of this which has a suggested approach about implementing it too.Brodeur
hi @jasonscript, this is actually now in latest version of the NuGet package :) It will retrieve option set labels in the FormattedValues array now, but if you still need to check them via a RetrieveAttributeRequest, then a good starting point would be this class RetrieveAttributeRequestExecutor, it's called automatically but the body throws a NotImplementedException for now.Brodeur
here's the linkBrodeur

© 2022 - 2024 — McMap. All rights reserved.