In This Topic
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 building 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# |
Copy Code |
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# |
Copy Code |
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 on the mapped classes, as demonstrated in the following code examples:
Note: For LINQ queries, the code must have declared "using System.Linq".
INSERT |
C# |
Copy Code |
public static void InsertD365S(AccountsContext context)
{
Console.WriteLine("Insert query started!");
// Create a new account
var account = new Account();
account.AccountId = newGuid();
account.Name = "New Account";
// Add account and save changes to the data source
context.Add(account);
context.SaveChanges();
Console.Write("Insert query executed successfully! \n\n");
} | |
SELECT |
C# |
Copy Code |
public static void ReadD365S(AccountsContext context)
{
Console.WriteLine("Select query started!");
// Define Select query
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# |
Copy Code |
public static void UpdateD365S(AccountsContext context)
{
Console.WriteLine("Update query started!");
// Retrieve thethe account that needs to be updated
Account account = (from p in context.Accounts
where p.Name == "New Account"
select p).FirstOrDefault();
// Update the required property
account.Name = "New Account updated";
context.SaveChanges();
Console.Write("Update query executed successfully! \n\n");
} | |
DELETE |
C# |
Copy Code |
public static void DeleteD365S(AccountsContext context)
{
Console.WriteLine("Deleting account...");
// Define the account to be deleted
Account account = context.Accounts.Where(x => x.Name.Equals("New Account updated")).FirstOrDefault();
// Delete the account and save changes to the data source
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 with the creation of the model in the Entity Framework.