DataConnector
ADO.NET provider for Salesforce / Entity Framework
In This Topic
    Entity Framework
    In This Topic

    The ADO.NET Provider for Salesforce supports Entity Framework which requires C1.EntityFrameworkCore.Salesforce 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 Salesforce.

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

    C#
    Copy Code
    public class Order
    {
        [Key]
        [DataMember(Name = "AccountId")]
        public String AccountId { get; set; }
        [DataMember(Name = "Status")]
        public String Status { get; set; }
        [DataMember(Name = "BillingState")]
        public String BillingState { get; set; }
        [DataMember(Name = "EffectiveDate")]
        public String EffectiveDate { get; set; }
       
    }
    

    Now, OrderContext class has been defined to access the Order datatable and establish a connection to the Salesforce service by overriding the OnConfiguring method. This method invokes the UseSalesforce method of DbContextOptionsBuilder class to configure the context and establish connection with Salesforce service.

    C#
    Copy Code
    public DbSet<Order> Order { get; set; }
    public OrderContext() : base()
    {
        Database.AutoTransactionsEnabled = false;
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        const string OAuthTokenEndpoint = @"****";
        const string Url = @"*****";
        const string ClientId = @"*****";
        const string ClientSecret = @"*****";
        const string Username = @"******";
        const string Password = @"*******";
        const string SecurityToken = @"";
        string connectionString = string.Format("Username={0};Password={1};Security Token={2};OAuth Client Id={3}; OAuth Client Secret={4}; OAuth Token Endpoint={5}; Url={6}; Use Pool = false; Max Page Size = 200", Username, Password, SecurityToken, ClientId, ClientSecret, OAuthTokenEndpoint, Url);
    
        optionsBuilder.UseSalesforce(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.

    C#
    Copy Code
        public static void SelectLinq(OrderContext context)
        {
            Console.WriteLine("Displaying Order Present in our collection...");
    
            var records = from p in context.Order
                          select p;
    
            foreach (var Order in records)
            {
                Console.WriteLine("{0} - {1} -{2} -{3}", Order.AccountId,Order.Status,Order.BillingState,Order.EffectiveDate);
            }
        }
        public static void Insert(OrderContext context)
        {
            Console.WriteLine("Adding New Order...");
            Order order1 = new Order();
            order1.AccountId = "0012w00000Ak4iLAAR";
            order1.Status = "Draft";
            order1.BillingState = "Delhi";
            order1.EffectiveDate = "23-Jan-1992";
            context.Order.Add(order1);//Adding
            context.SaveChanges();//Save changes to the data source
            Console.WriteLine("New book added successfully.. \n \n");
        }
        public static void Delete(OrderContext context)
        {
            Console.WriteLine("Delete Started...");
            var order1 = context.Order.Last();
            context.Order.Remove(order1);//Deleting
            context.SaveChanges();//Save changes to the data source
            Console.WriteLine("Deleted Successful... \n \n ");
        }
        public static void Update(OrderContext context)
        {
            Console.WriteLine("Update Started... ");
            Order order1 = (from p in context.Order
                       where p.AccountId == "0012w00000Ak4iLAAR"
                          select p).FirstOrDefault(); //Retriving Order that need to be update
            order1.BillingState = "Delhi";//Updated the required property or attribute
            context.SaveChanges();
            Console.WriteLine("Update Successful...\n \n ");
        }
    }
    
    Note: Scaffolding feature supports user to create the model and dbcontext when you create model in the Entity Framework for all dataconnectors.