[]
The ADO.NET Provider for Salesforce supports Entity Framework which requires C1.EntityFrameworkCore.Salesforce package to be installed. In this section, the model first approach is used to build an Entity Framework model that maps data tables to classes for simpler access to Salesforce.
The following code defines the Order class to map the Order datatable.
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; }
}
The next code example defines OrderContext class 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 the DbContextOptionsBuilder class to configure the context and establish a connection with the Salesforce service.
public DbSet<Order> Order { get; set; }
public OrderContext() : base()
{
Database.AutoTransactionsEnabled = false;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
const string OAuthTokenEndpoint = @"https://*****/****/****/****";
const string Url = @"https://*****/****/****/****";
const string ClientId = @"yourID";
const string ClientSecret = @"yourCSecret";
const string Username = @"yourUsername";
const string Password = @"yourPassword";
const string SecurityToken = @"yourToken";
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: For LINQ queries, "using System.Linq" must be declared in the code.
INSERT
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 = DateTime.Now;
context.Order.Add(order1);//Adding
context.SaveChanges();//Save changes to the data source
Console.WriteLine("New book added successfully.. \n \n");
}
SELECT
public static void Select(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);
}
}
UPDATE
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 ");
}
DELETE
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 ");
}
Note: The Scaffolding feature supports the user to create the model and dbcontext when you create a model in the Entity Framework for all dataconnectors.