Jump to content

.NET Core 8 - Microsoft Dataverse Map to Objects

Hi all,

 

I have created a test application for connectin to a Dataverse. I am able to retireve data from tables but I was wondering if someone knows the best way to map table data to objects.

 

Quite new to the dataverse so any help be greatly appreicated.

if (serviceClient != null)
{
    // Define the query expression to retrieve the top 1 record
    QueryExpression query = new QueryExpression
    {
        EntityName = queryTable,
        ColumnSet = new ColumnSet(true), // Retrieve all columns, or specify particular columns
        PageInfo = new PagingInfo
        {
            PageNumber = 1,
            Count = 3 // Limit to top 1 record
        }
    };

    // Execute the query
    EntityCollection result = serviceClient.RetrieveMultiple(query);

    for (int i = 0; i < result.Entities.Count; i++)
    {
        if (result[i] != null)
        {
            Console.WriteLine(test);
            Entity record = result[i];
            Console.WriteLine($"Item Index: {i}");
            foreach (var attribute in record.Attributes)
            {

                Console.WriteLine($"{attribute.Key}: {attribute.Value}");
            }
        }
        else
        {
            Console.WriteLine($"No records found in the {queryTable} table.");
        }
    }
}

 

Link to post
Share on other sites

You can always use AutoMapper. Although i never had good result as i always need way too much customization down the line that custom made one is very easy as well.

 

For custom just load either XML or JSON so you can add the element attribute of either. Name your element from the DB column name.

 

When actually loading data you will have 2 things, the source from the DB/Object store and the Object Datatype/class.

Create a dictionary that will contain a list of object with their reflected properties and if the reflection was already done for the given class/object you will find it in the dictionary so use it else build it, else build the reflection and store it for the next object to be faster and use it.

 

Loop on the properties from the reflection and apply the proper cast / convert depending on the type and the object will be loaded.

 

This solution allow to control converters to apply much better as custom converters can be created in the classes themselves an they can override default converters.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×