EntityMapper.Dynamics.CRM 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityMapper.Dynamics.CRM --version 1.0.0
NuGet\Install-Package EntityMapper.Dynamics.CRM -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityMapper.Dynamics.CRM" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityMapper.Dynamics.CRM --version 1.0.0
#r "nuget: EntityMapper.Dynamics.CRM, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityMapper.Dynamics.CRM as a Cake Addin
#addin nuget:?package=EntityMapper.Dynamics.CRM&version=1.0.0

// Install EntityMapper.Dynamics.CRM as a Cake Tool
#tool nuget:?package=EntityMapper.Dynamics.CRM&version=1.0.0

Entity Mapper

Entity mapper is an attribute based mapper for Microsoft Dynamics CRM SDK.

Existing methods

There are two programming models that you can choose from Early bound and Late bound.

Late bound method

Use the code generation tool (CrmSvcUtil) to create early-bound entity classes, derived from the Entity class, which you can use to access business data in Dynamics 365 Customer Engagement. These classes include one class for each entity in your installation, including custom entities. More information: Use the early bound entity classes in code

Early bound method

The Entity class contains the logical name of an entity and a property-bag array of the entity's attributes. This lets you use late binding so that you can work with types such as custom entities and custom attributes that were not present when your application was compiled. More information: Use the late bound entity class in code

Entity Mapper method

Lets you enjoy the stability of a strong typed model and the agility of a property-bag array.

Usage

The "CrmEntityBase" Class

This basic class holds only two attributes

LogicalName (the schema name for the sorce entity) Id (as you know every entity in dynamics crm has one)

Every model you build has to inherit from this class.

public abstract class CrmEntityBase
{
    public abstract string LogicalName { get; }
    public Guid Id { get; set; }
}

A Basic Model

public class ContactModel : CrmEntityBase
{
    public override string LogicalName => "contact";

    [CRMField("fullname")]
    public string FullName { get; set; }

    [CRMLookup("parentaccountid", "account")]
    public Guid? ParentAccountId { get; set; }
}

Mapping From Entity

var contactEntity = new Entity("contact")
{
    ["fullname"] = "John Doe",
    ["parentaccountid"] = new EntityReference("account", Guid.Empty)
};

var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.FullName);
Console.WriteLine(contactModel.ParentAccountId.ToString());

// The example displays the following output:
//  John Doe
//  00000000-0000-0000-0000-000000000000

Mapping To Entity

var contactModel = new ContactModel
{
    FullName = "Bar Refaeli",
    ParentAccountId = Guid.Empty,
};

var contactEntity = Mapper.ToEntity(contactModel);
var fullname = contactEntity.GetAttributeValue<string>("fullname");
var parentAccountRef = contactEntity.GetAttributeValue<EntityReference>("parentaccountid");

Console.WriteLine(fullname);
Console.WriteLine(parentAccountRef.LogicalName);
Console.WriteLine(parentAccountRef.Id);

// The example displays the following output:
//  Bar Refaeli
//  account
//  00000000-0000-0000-0000-000000000000

Getting The Required ColumnSet

var contactColumnSet = Mapper.GetColumnSet<ContactModel>();

Console.WriteLine(string.Join(", ",contactColumnSet.Columns));

// The example displays the following output:
//  fullname, parentaccountid

CRMFieldType

This is the type of the field that we are declaring. It has the following values:

  1. Basic - A general map string > string, int > int, etc..
  2. OptionSet - int get / set the value of OptionSet
  3. Lookup - Guid get / set the id of the referenced target
  4. Money - decimal get / set the value of the Money object
  5. FormattedValue - string get (only) the formatted value
  6. Enum - Enum get / set the value of OptionSet (using its numeric value)
  7. LookupName - string get the reference name.

Attributes Examples

Lookup

public class ContactModel : CrmEntityBase
{
    ...
    [CRMLookup("parentaccountid", "account")]
    public Guid? ParentAccountId { get; set; }
    ...
}
var contactModel = new ContactModel
{
    ParentAccountId = Guid.Empty,
};

var contactEntity = Mapper.ToEntity(contactModel);
var parentAccountRef = contactEntity.GetAttributeValue<EntityReference>("parentaccountid");
var contactModelAgain = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(parentAccountRef.LogicalName);
Console.WriteLine(parentAccountRef.Id);
Console.WriteLine(contactModelAgain.ParentAccountId);

// The example displays the following output:
//  account
//  00000000-0000-0000-0000-000000000000
//  00000000-0000-0000-0000-000000000000

FormattedValue

public class ContactModel : CrmEntityBase
{
    ...
    [CRMField("parentaccountid", CRMFieldType.FormattedValue)]
    public string ParentAccountIdName { get; set; }
    ...
}
var contactEntity = new Entity("contact", Guid.NewGuid())
{
    ["parentaccountid"] = new EntityReference("account", Guid.Empty)
};

// faking formatted value
contactEntity.FormattedValues["parentaccountid"] = "Mama Theresa";
var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.ParentAccountIdName);

// The example displays the following output:
//  Mama Theresa

Enum

public enum ContactMood
{
    Sad = 0,
    Tired = 1,
    Happy = 2,
    Coding = 14,
}

public class ContactModel : CrmEntityBase
{
    ...
    [CRMEnum("new_mood", typeof(ContactMood?))]
    public ContactMood? Mood { get; set; }
    ...
}
var contactModel = new ContactModel
{
    FullName = "Bar Refaeli",
    ParentAccountId = Guid.Empty,
    Mood = ContactMood.Coding
};

var contactEntity = Mapper.ToEntity(contactModel);
var contactState = contactEntity.GetAttributeValue<OptionSetValue>("new_mood");
var contactModelAgain = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactState.Value);
Console.WriteLine(contactModelAgain.Mood);
Console.WriteLine(contactModelAgain.Mood.GetType().Name);

// The example displays the following output:
//  14
//  Coding
//  ContactMood

LookupName

public class ContactModel : CrmEntityBase
{
    ...
    [CRMField("parentaccountid", CRMFieldType.LookupName)]
    public string ParentAccountIdName { get; set; }
    ...
}
var accountRef = new EntityReference("account", Guid.Empty)
{
    Name = "Mama Theresa"
};

var contactEntity = new Entity("contact", Guid.Empty)
{
    ["parentaccountid"] = accountRef,
};

var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.ParentAccountIdName);

// The example displays the following output:
//  Mama Theresa

Working With Aliased Values

In development

Product Compatible and additional computed target framework versions.
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0-alpha2 1,047 6/4/2023
2.0.0-alpha1 558 1/24/2023
1.1.0.1 721 1/20/2023
1.1.0 694 1/20/2023
1.0.7 730 8/15/2021
1.0.6 783 6/18/2021
1.0.5 718 6/18/2021
1.0.4 761 6/18/2021
1.0.3 742 6/18/2021
1.0.2 1,748 6/18/2021
1.0.1 827 3/13/2021
1.0.0 755 3/12/2021

Initial Release