linqpluginsdataversepower-platformpowerapps-modeldriven

Multiple Dataverse plugin assemblies inside Nuget package and LINQ query issue


I have the following situation on my Dataverse environment. I’ve a Visual Studio Dataverse plugin solution which contains 4 separate .NET 4.6.2 projects. My data model classes (generated with PAC CLI tool) are stored in one of the above-mentioned projects and are built into separate assembly which is used by other 3 DLLs. Context class is marked with Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute attribute.

Components architecture of this solution looks like on the screen below: enter image description here

The whole solution is released into Dataverse as a NuGet plugin package (containing 4 DLLs mentioned above). When the following LINQ query is executed inside a plugin everything works fine.

    public Account GetByAccountNumber(string number)
    {
        using (var ctx = new DataverseContext(this.service) { MergeOption = MergeOption.NoTracking })
        {
            return ctx.CreateQuery<Account>()
                .Where(x => x.AccountNumber == number)
                .FirstOrDefault();
        }
    }

However, when I run similar query against SystemUser table or any kind of custom tables – I’m getting the following error:

Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'XXX.Customer.Project.Model.SystemUser'

This is a very interesting situation because for account table I’m not getting this error. It happens only when I try to use LINQ query against SystemUser or custom tables. I’ve tried to merge all the 4 assemblies into single one and it eliminates the problem, but it does not make sense for me to use NuGet package and store single merged assembly inside.

Has anyone met with the similar situation?

Any ideas?


Solution

  • Ok. I believe the following code, used during OrganizationServiceFactory initialization, solved the problem:

    var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
    var proxyProvider = serviceFactory as IProxyTypesAssemblyProvider;
    if(proxyProvider != null)
    {
        proxyProvider.ProxyTypesAssembly = typeof(DataverseContext).Assembly;
    }
    

    Where DataverseContext is the name of my strong-typed tables classes context.