Forum Discussion

Mariano1978's avatar
Mariano1978
Copper Contributor
May 23, 2022

Create a record from a c# console app using OData entity

Hello,

I'm creating a simple demo for creating a new record using an entity and a c# console app, Dynamics 365 FO will expose the entity API, because I'm using Visual Studio 2019 instead of using "OData Client Code Generator" I'm using "https://docs.microsoft.com/en-us/odata/client/code-generation-tool" wich will create the client proxy files for the OData services, the problem I'm facing is that whenever I use a DataServiceCollection object with the Dynamics 365 FO's entity CustomerGroup an error is thrown "The element type 'Microsoft.Dynamics.DataEntities.CustomerGroup' does not have any key property.'", but if I take a look at the proxy file I can see the key property for the entity:

 

 

my code is quite simple:

 

 

 

 

 

 

public static void CreateCustomerGroup(DataServiceContext _context)
{
    try
    {
     
        CustomerGroup customerGroup = new CustomerGroup
        {
            DataAreaId = "cgc",
            CustomerGroupId = "OData",
            Description = "INSERT FROM ODATA"
        };

        //Once the next line is hit the error "The element type 'Microsoft.Dynamics.DataEntities.CustomerGroup' does not have any key property." is thrown 
        DataServiceCollection<CustomerGroup> customerCollection = new DataServiceCollection<CustomerGroup>(_context)
        {
            customerGroup
        };

        _context.SaveChanges();

        Console.WriteLine(string.Format("Customer group created"));
    }
    catch (DataServiceRequestException e)
    {

        Console.WriteLine(string.Format("error creating customer group "));
    }
}

 

 

 

 

I don't understant why if I can see the key property in the proxy file the error is complaning about that

 

1 Reply

  • LanHuang's avatar
    LanHuang
    Former Employee

    Hi Mariano1978,

    Thanks for posting your issue here.

    However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
    Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
    Best Regards,
    Lan Huang

Resources