Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Classes can inherit from FuseIT.S4D.DynamicsConnector.Entities.EntityBase to define strongly-typed entity objects.

using FuseIT.S4D.DynamicsConnector.Metadata;
// The Entity attribute defines the Dynamics 365 entity name.
[Entity("contact")]
class Contact : EntityBase<Contact>
{
  // Makes the emailaddress1 field available as an object property, with a type.
  public string email
  {
    get => (string)this["emailaddress1"];
    set { this["emailaddress1"] = value; }
  }
  public string firstname
  {
    get => (string)this["firstname"];
    set { this["firstname"] = value; }
  }
  public string lastname
  {
    get => (string)this["lastname"];
    set { this["lastname"] = value; }
  }
  // Any fields not explicitly defined are still accessible via the Dictionary syntax.
  // e.g. this["contactid"]
}

This also allows use of alternate query expression syntax.

var session = serviceFactory.GetSessionFromConnectionString("MyDynamicsInstance");
// Find a contact by email address.
var contact = await Contact.LoadSingleAsync(session,
  (query, exp) =>
  {
    query.Filter(exp.email == "example@example.com").Select(exp.firstname, exp.lastname);
  }
);
// Returned objects have strongly-type fields where defined.
Console.WriteLine($"First name: {contact.firstname}, Last name: {contact.lastname}");
  • No labels