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.
C# |
コードのコピー
|
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 DbContextOptionsBuilder class to configure the context and establish connection with Salesforce service.
C# |
コードのコピー
|
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 |
C# |
コードのコピー
|
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);//追加します。
context.SaveChanges();//データソースに変更を保存します。
Console.WriteLine("New book added successfully.. \n \n");
}
|
|
SELECT |
C# |
コードのコピー
|
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 |
C# |
コードのコピー
|
public static void Update(OrderContext context)
{
Console.WriteLine("Update Started... ");
Order order1 = (from p in context.Order
where p.AccountId == "0012w00000Ak4iLAAR"
select p).FirstOrDefault(); //更新する必要がある注文を取得します。
order1.BillingState = "Delhi";//必要なプロパティまたは属性を更新します。
context.SaveChanges();
Console.WriteLine("Update Successful...\n \n ");
}
|
|
DELETE |
C# |
コードのコピー
|
public static void Delete(OrderContext context)
{
Console.WriteLine("Delete Started...");
var order1 = context.Order.Last();
context.Order.Remove(order1);//削除します
context.SaveChanges();//データソースに変更を保存します。
Console.WriteLine("Deleted 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.