DataConnector
ADO.NET provider for Dynamics 365 Sales / Entity Framework
In This Topic
    Entity Framework
    In This Topic

    The ADO.NET Provider for OData supports Entity Framework which requires C1.EntityFrameworkCore.D365S package to be installed. In this section, we have used the model first approach to build an Entity Framework model that maps data tables to classes for simpler access to Dynamics 365 Sales.

    The below code example defines the Account class to map the Account datatable.

    public class Account
    {
      [Key]
      [Column("AccountId")]
      public Guid AccountId { get; set; }
      [Column("Name")]
      public string Name { get; set; }
    }
    

    Now, AccountsContext class has been defined to access the Account datatable and establish a connection to the Dynamic365 Sales service by overriding the OnConfiguring method. This method invokes the UseD365S method of DbContextOptionsBuilder class to configure the context and establish connection with Dynamic365 Sales service.

    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);
    }
    

    You can now use the LINQ queries to perform different data operations to the mapped classes as demonstrated in the code below. Note that "using System.Linq" must be declared in your code.

    public static void Insert365(AccountsContext context)
    {
       Console.WriteLine("Insert query started !!!");
       //Defining Insert Query
       Account account = new Account();
       account.AccountId = new Guid();
       account.Name = "Reli Account";
       context.Add(account);
       //Save changes to database
       context.SaveChanges();
       Console.Write("Insert query executed successsfuly !!! \n \n");
    }
    public static void Read365(AccountsContext context)
    {
       Console.WriteLine("Read query started !!!");
       //Defining read 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("Read query executed successsfuly !!! \n \n");
    }
    public static void Update365(AccountsContext context)
    {
       Console.WriteLine("Update query started !!!");
       //Defining update query
       Account account = (from p in context.Accounts
                select p).First(); 
       account.Name = "Johsihna";
       //Save changes to database
       context.SaveChanges();
                
       Console.Write("Update query executed successsfuly !!! \n \n");
    }       
    public static void Delete365(AccountsContext context)
    {
       Console.WriteLine("Delete query started !!!");
       //Defining Delete query
       Account account = context.Accounts.Last();
       Console.Write("Account Deleted :" + account.Name +"\n");
       context.Accounts.Remove(account);
       //Save changes to database
       context.SaveChanges();
       Console.Write("Delete query executed successsfuly !!! \n \n");
    }
    
    Note: Scaffolding feature supports user to create the model and dbcontext when you create model in the Entity Framework for all dataconnectors.