Entity mapper is an attribute based mapper for Microsoft Dynamics CRM SDK.
There are two programming models that you can choose from Early bound and Late bound.
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
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
Lets you enjoy the stability of a strong typed model and the agility of a property-bag array.
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; }
}
public class ContactModel : CrmEntityBase
{
public override string LogicalName => "contact";
[CRMField("fullname")]
public string FullName { get; set; }
[CRMLookup("parentaccountid", "account")]
public Guid? ParentAccountId { get; set; }
}
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
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
var contactColumnSet = Mapper.GetColumnSet<ContactModel>();
Console.WriteLine(string.Join(", ",contactColumnSet.Columns));
// The example displays the following output:
// fullname, parentaccountid
This is the type of the field that we are declaring. It has the following values:
- Basic - A general map
string
>string
,int
>int
, etc.. - OptionSet -
int
get / set the value of OptionSet - Lookup -
Guid
get / set the id of the referenced target - Money -
decimal
get / set the value of the Money object - FormattedValue -
string
get (only) the formatted value - Enum -
Enum
get / set the value of OptionSet (using its numeric value) - LookupName -
string
get the reference name.
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
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
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
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
In development