DataConnector
Entity Framework
ADO.NET provider for Dynamics 365 Sales > Entity Framework

The ADO.NET Provider for Dynamics 365 Sales supports Entity Framework through the C1.EntityFrameworkCore.D365S package, which needs to be installed in addition. This article demonstrates the model-first approach to build an Entity Framework model that maps data tables to classes for simpler access to Dynamics 365 Sales.

The following code example defines the Account class as mapping the Account datatable:

C#
コードのコピー
public class Account
{
    [Key]
    [Column("AccountId")]
    public Guid AccountId { get; set; }
    [Column("Name")]
    public string Name { get; set; }
}

The next code example defines AccountsContext class to access the Account datatable. This is done by overriding the OnConfiguring method, which invokes the UseD365S method of the passed DbContextOptionsBuilder object, to configure the context and establish a connection to the Dynamics 365 Sales service.

C#
コードのコピー
public string ConnectionString;
public DbSet<Account> Accounts { get; set; }
public AccountsContext(string connectionString) : base()
{
    ConnectionString = connectionString;
    Database.AutoTransactionsEnabled = false;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseD365S(ConnectionString);
}

The LINQ queries can now be used to perform various data operations to the mapped classes, as demonstrated in the following code examples:

Note: For LINQ queries, the code must have declared "using System.Linq".
INSERT
C#
コードのコピー
public static void InsertD365S(AccountsContext context)
{
    Console.WriteLine("Insert query started!");

    // 新しいアカウントを作成します。
    var account = new Account();
    account.AccountId = newGuid();
    account.Name = "New Account";

    // アカウントを追加し、データ ソースへの変更を保存します。
    context.Add(account);
    context.SaveChanges();

    Console.Write("Insert query executed successfully! \n\n");
}
SELECT
C#
コードのコピー
public static void ReadD365S(AccountsContext context)
{
    Console.WriteLine("Select query started!");

    // Select クエリを定義します。
    var records = (from p in context.Accounts 
                   select p
                   ).Take(10);

    foreach (var account in records)
    {
        Console.WriteLine("{0} - {1}", account.AccountId, account.Name);
    }

    Console.Write("Select query executed successfully! \n\n");
}
UPDATE
C#
コードのコピー
public static void UpdateD365S(AccountsContext context)
{
    Console.WriteLine("Update query started!");

    // 更新する必要があるアカウントを取得します。
    Account account = (from p in context.Accounts
                        where p.Name == "New Account"
                        select p).FirstOrDefault();

    // 必要なプロパティを更新します。
    account.Name = "New Account updated";
    context.SaveChanges();

    Console.Write("Update query executed successfully! \n\n");
}
DELETE
C#
コードのコピー
public static void DeleteD365S(AccountsContext context)
{
    Console.WriteLine("Deleting account...");

    // 削除するアカウントを定義します。
    Account account = context.Accounts.Where(x => x.Name.Equals("New Account updated")).FirstOrDefault();

    // アカウントを削除し、データ ソースへの変更を保存します。
    context.Accounts.Remove(account);
    context.SaveChanges();

    Console.Write("Account deleted: " + account.Name + "\n");
}
Note: For all dataconnectors, the Scaffolding feature supports the creation of the model and the dbcontext along the creation of the model in the Entity Framework.