Skip to content

Instantly share code, notes, and snippets.

@fraga
Created October 18, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fraga/3e2f84ff51367c1e02b5fe4d29a77392 to your computer and use it in GitHub Desktop.
Save fraga/3e2f84ff51367c1e02b5fe4d29a77392 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using ODataUtility.Microsoft.Dynamics.DataEntities;
using System;
using System.Linq;
using Microsoft.OData.Client;
namespace ODataConsoleApplication
{
class SimpleCRUDExamples
{
public static void SimpleDOLDsapUpdate(Resources d365Context)
{
// -----------------------
// TEST #1
// -----------------------
//DataServiceQuery<Dcm_DE_DOLProdTableExchange> qry = d365Context.Dcm_DE_DOLProdTableExchange.AddQueryOption("$filter", "dataAreaId eq '20'").AddQueryOption("cross-company", "true");
DataServiceQuery<Dcm_DE_DOLProdTableExchange> qry = d365Context.Dcm_DE_DOLProdTableExchange.AddQueryOption("cross-company", "true");
foreach (var q in qry)
{
//d365Context.AttachTo("Dcm_DE_DOLProdTableExchange", q);
System.Diagnostics.Debug.WriteLine(q.BatchId);
q.DOL_DSAP = "1";
q.DOL_DSAP_Updated = NoYes.Yes;
d365Context.UpdateObject(qry);
d365Context.SaveChanges();
}
// -----------------------
// TEST #2
// -----------------------
Dcm_DE_DOLProdTableExchange qry2 = d365Context.Dcm_DE_DOLProdTableExchange.AddQueryOption("$filter", "dataAreaId eq '20'").AddQueryOption("cross-company", "true").First();
qry2.DOL_DSAP = "1";
qry2.DOL_DSAP_Updated = NoYes.Yes;
d365Context.UpdateObject(qry2);
d365Context.SaveChanges();
// -----------------------
// TEST #3
// -----------------------
Dcm_DE_DOLProdTableExchange qry3 = d365Context.Dcm_DE_DOLProdTableExchange.AddQueryOption("$filter", "dataAreaId eq '20' and BatchId eq 'D200000335'").AddQueryOption("cross-company", "true").First();
qry3.DOL_DSAP = "1";
qry3.DOL_DSAP_Updated = NoYes.Yes;
d365Context.UpdateObject(qry3);
d365Context.SaveChanges();
// -----------------------
// TEST #4
// -----------------------
var qry4 = d365Context.Dcm_DE_DOLProdTableExchange.First();
qry4.DOL_DSAP = "1";
qry4.DOL_DSAP_Updated = NoYes.Yes;
d365Context.UpdateObject(qry4);
d365Context.SaveChanges();
}
public static void SimpleCRUD(Resources d365)
{
string assetMajorTypeId = "Test01";
string company = "20";
// Create
d365.AddToAssetMajorTypes(new AssetMajorType
{
DataAreaId = company,
MajorTypeId = assetMajorTypeId,
Description = "Description of Test01"
});
d365.SaveChanges();
// Read
var assetMajorType = d365.AssetMajorTypes.Where(x => x.DataAreaId == company && x.MajorTypeId == assetMajorTypeId).First();
Console.WriteLine(JsonConvert.SerializeObject(assetMajorType));
Console.WriteLine("Asset Major type of ID {0} successfully created and read.", assetMajorType.MajorTypeId);
// Update
assetMajorType.Description = "Updated description";
d365.UpdateObject(assetMajorType);
d365.SaveChanges();
var assetMajorTypeAfterUpdate = d365.AssetMajorTypes.Where(x => x.DataAreaId == company && x.MajorTypeId == assetMajorTypeId).First();
Console.WriteLine(JsonConvert.SerializeObject(assetMajorTypeAfterUpdate));
Console.WriteLine("Asset Major type of ID {0} successfully updated.", assetMajorTypeAfterUpdate.MajorTypeId);
// Delete
d365.DeleteObject(assetMajorTypeAfterUpdate);
d365.SaveChanges();
var assetMajorTypeAfterDelete = d365.AssetMajorTypes.Where(x => x.DataAreaId == company && x.MajorTypeId == assetMajorTypeId);
Console.WriteLine("Records found = {0}", assetMajorTypeAfterDelete.Count());
Console.WriteLine("Asset Major type of ID {0} successfully deleted.", assetMajorTypeAfterUpdate.MajorTypeId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment