Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Issue retrieving duplicates in Odata objects

(0) ShareShare
ReportReport
Posted on by 300

I have a trade journal with two lines.

I've created a custom data entity to show both of these lines with some of the parent information. Below is a screenshot of the data entity, all looks well!!

D365json.PNG

However when I pull the same entity from that same Odata endpoint with my c# application I get two rows, but the second row is a duplicate of the first!

pastedimage1584366020626v1.png

pastedimage1584366058439v2.png

C# code to call the odata endpoint is as follows

public IEnumerable PullPLMPriceDiscAdms(string dataAreaId)
        {
            lock (_oDatalock)
            {
                try
                {
                    var context = GetContext();
                    var discAdms = context.PLMPriceDiscAdms.AddQueryOption("$filter", "dataAreaId eq '"   dataAreaId   "' and Posted eq Microsoft.Dynamics.DataEntities.NoYes'No'")
                        .AddQueryOption("cross-company", "true").ToList();

                    return discAdms.Where(x => x.DefaultRelation == PriceType.PriceSales);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        
         public Resources GetContext()
        {
            string oDataEntityPath = $"{_profile.UriString.Trim('/')}{UriStringSuffix}";
            Uri oDataUri = new Uri(oDataEntityPath, UriKind.Absolute);
            var context = new Resources(oDataUri);
            OAuthHelper.Init(_profile);

            context.SendingRequest2  = new EventHandler(delegate (object sender, SendingRequest2EventArgs e)
            {
                var response = Task.Run(async () =>
                {
                    return await OAuthHelper.GetAuthenticationHeader();
                });

                response.Wait();

                e.RequestMessage.SetHeader(OAuthHelper.OAuthHeader, response.Result.ToString());
            });
            return context;
        }

Im using a connected service (Unchase.OData.ConnectedService) to create the classes, and the following DLLs to make the connection

Microsoft.OData.Client

Microsoft.OData.Core

Microsoft.OData.Edm

Has anyone seen anything like this before, or got any ideas where to look? 

  • MichaelZ Profile Picture
    MichaelZ 2 on at
    RE: Issue retrieving duplicates in Odata objects

    I believe I found the issue and wanted to post it for anyone who stumbles upon this in the future.  

    The assumption I am taking is that you used the OData Generator to create the ODataClient and EDMX data for the entities.

    In my case we had a custom view that was created which linked a few tables together and exposed that data via an entity.  This enabled us to consume it externally, but we kept seeing random issues on a custom application.  We would step through it and even though the data in a web browser or postman was valid it wasn't valid in the custom application.  

    Once I had discovered this entity is using an underlying view and due to my lack of knowledge on development practices. I opted to NOT change anything on the entity/view.  

    Upon further digging a lot of people are suggesting that the METADATA that supplied the ODataClient and EDMX data was the source of the issue.  Upon looking at the file and searching for the entity in question it was immediately obvious that this was the issue because it had only detected 3 of the 5 primary keys.  I manually edited the generated ODataClient.cs in a few spots

    1. Updated Key list

       [global::Microsoft.OData.Client.Key("primary", "column", "name")]

       [global::Microsoft.OData.Client.EntitySet("ABCEntityName")]

       [global::Microsoft.OData.Client.OriginalNameAttribute("ABCEntityName")]

       public partial class ABCEntityName : global::Microsoft.OData.Client.BaseEntityType, global::System.ComponentModel.INotifyPropertyChanged

    specifically the first line was change from [global::Microsoft.OData.Client.Key("primary", "column", "name") to [global::Microsoft.OData.Client.Key("primary", "column", "name", "goes", "here")

    2. Updated Constructor

    From:

           [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "7.5.1")]

           public static ABCEntityName CreateABCEntityName(string primary,

                       string column,

                       string name)

           {

               ABCEntityName aBCEntityName = new ABCEntityName();

               aBCEntityName.primary = primary;

               aBCEntityName.column = column;

               aBCEntityName.name = name;

               return aBCEntityName ;

           }

    To:

           [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "7.5.1")]

           public static ABCEntityName CreateABCEntityName(string primary,

                       string column,

                       string name,

                       string goes,

                       string here)

           {

               ABCEntityName aBCEntityName = new ABCEntityName();

               aBCEntityName.primary = primary;

               aBCEntityName.column = column;

               aBCEntityName.name = name;

               aBCEntityName.name = goes;

               aBCEntityName.name = here;

               return aBCEntityName ;

           }

    3. Updated

    From:

    public static global::ODataUtility.Microsoft.Dynamics.DataEntities.ABCEntityNameSingle ByKey(this global::Microsoft.OData.Client.DataServiceQuery<global::ODataUtility.Microsoft.Dynamics.DataEntities.ABCEntityName> source,

               string primary,

               string column,

               string name,

           {

               global::System.Collections.Generic.Dictionary<string, object> keys = new global::System.Collections.Generic.Dictionary<string, object>

               {

                   { "primary", primary },

                   { "column", column },

                   { "name", name },

               };

    To:

    public static global::ODataUtility.Microsoft.Dynamics.DataEntities.ABCEntityNameSingle ByKey(this global::Microsoft.OData.Client.DataServiceQuery<global::ODataUtility.Microsoft.Dynamics.DataEntities.ABCEntityName> source,

               string primary,

               string column,

               string name,

               string goes,

               string here)

           {

               global::System.Collections.Generic.Dictionary<string, object> keys = new global::System.Collections.Generic.Dictionary<string, object>

               {

                   { "primary", primary },

                   { "column", column },

                   { "name", name },

                   { "goes", goes },

                   { "here", here }

               };

    4. Update EDMX Data

    From:

    <EntityType Name=""ABCEntityName"">

           <Key>

             <PropertyRef Name=""primary"" />

             <PropertyRef Name=""column"" />

             <PropertyRef Name=""name"" />

           </Key>

    To:

    <EntityType Name=""ABCEntityName"">

           <Key>

             <PropertyRef Name=""primary"" />

             <PropertyRef Name=""column"" />

             <PropertyRef Name=""name"" />

             <PropertyRef Name=""goes"" />

             <PropertyRef Name=""here"" />

           </Key>

  • MichaelZ Profile Picture
    MichaelZ 2 on at
    RE: Issue retrieving duplicates in Odata objects

    Did you ever find the source of this issue?  I am seeing the exact same problem.

  • RE: Issue retrieving duplicates in Odata objects

    Hi, Like Nikolaos mentioned, what is the result when you make the oData call via the browser or another tool like Postman or powershell? Browser is best because we know there isnt any code to manipulate anything but just a get request.

  • richierich79 Profile Picture
    richierich79 300 on at
    RE: Issue retrieving duplicates in Odata objects

    I've added more detail, as well as larger images

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Issue retrieving duplicates in Odata objects

    I can't see the second screenshot (it's too small), but assuming the OData feed in your browser works correct, the problem must be in your C# code. So please check it carefully. You can also share it here if you wish to get our feedback.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans