Reputation: 686
I am trying to create a simple application where I am querying against entity, getting results, running the results through a web service and getting the missing information. I am stuck at making updating the database with the new and updated results here is the code I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetexV2
{
class Program
{
static void Main(string[] args)
{
string tid = "somevalue".ToString();
//webservice related
VertexWebService.PostalAddressType pat = new VertexWebService.PostalAddressType();
VertexWebService.VertexEnvelope env = new VertexWebService.VertexEnvelope();
VertexWebService.lookupTaxAreasRequest LTAR = new VertexWebService.lookupTaxAreasRequest();
VertexWebService.LookupTaxAreasWS60Client soap = new VertexWebService.LookupTaxAreasWS60Client();
VertexWebService.LoginType log = new VertexWebService.LoginType();
VertexWebService.TaxAreaLookupType talt = new VertexWebService.TaxAreaLookupType();
VertexWebService.TaxAreaRequestType tart1 = new VertexWebService.TaxAreaRequestType();
log.TrustedId = tid;
using (var db = new VetexV2.pesqlshareEntities1())
{
// query against the database
var query = from b in db.SalesOrder_FromSF
where b.VertexGeoCode.Length == 1
select new { address1 = b.Address1,
address2 = b.Address2,
city = b.JobCity,
state = b.StateCode,
zipcode = b.JobZip,
country = b.Country,
TAID = b.VertexGeoCode,
SalesforceOpportunity = b.SF_OpportunityID};
//parsing through the result
foreach (var item in query)
{
Console.WriteLine(item.address1);
Console.WriteLine(item.address2);
Console.WriteLine(item.city);
Console.WriteLine(item.state);
Console.WriteLine(item.zipcode);
Console.WriteLine(item.TAID);
pat.PostalCode = item.zipcode;
pat.MainDivision = item.state;
pat.Country = item.country;
pat.City = item.city;
pat.StreetAddress1 = item.address1;
pat.StreetAddress2 = item.address2;
talt.Item = pat;
tart1.TaxAreaLookup = talt;
env.Item = tart1;
env.Login = log;
env.Item = tart1;
LTAR.VertexEnvelope = env;
//using the info from above providing it to websevice
soap.Open();
soap.LookupTaxAreas60(ref LTAR.VertexEnvelope);
var reslt = ((VertexWebService.TaxAreaResponseType)(LTAR.VertexEnvelope.Item)).TaxAreaResult[0].taxAreaId.ToString();
Console.WriteLine(reslt);// displaying the missing or updated field on screen
Console.WriteLine("Press any Key");
// how do I put this updated field back into database ?
}
}
}
}
}
Upvotes: 0
Views: 217
Reputation: 686
I found another way by using
using (TransactionScope scope = new TransactionScope())
{
using ( var db= new dbEntity)
{
//query code
Foreach ( var item in query)
{
//program logic
db.savechange();
}
}
scope.complete();
}
by doing above I was able to do the save within foreach loop and save the transction i needed to make sure that had primary key and also needed to reference the system.Transctions
Upvotes: 0
Reputation: 5550
As I read your code correctly I'm making some assumptions:
Instead of making an anonymous type in your query, just select the entity itself:
var query = from b in db.SalesOrder_FromSF
where b.VertexGeoCode.Length==1
select b;
Assign your webservice objects directly to the entity properties and push back in the result. Then just call SaveChanges()
on the dbContext:
using (var db = new VetexV2.pesqlshareEntities1())
{
// query code as above
// other possible code...
foreach (var item in query)
{
pat.PostalCode = item.JobZip;
pat.MainDivision = item.StateCode;
pat.Country = item.Country;
pat.City = item.JobCity;
pat.StreetAddress1 = item.Address1;
pat.StreetAddress2 = item.Address2;
//... other code omitted for brevity
soap.LookupTaxAreas60(ref LTAR.VertexEnvelope);
var reslt = ((VertexWebService.TaxAreaResponseType)(LTAR.VertexEnvelope.Item))
.TaxAreaResult[0].taxAreaId.ToString();
item.VertexGeoCode = reslt;
// other code...
}
db.SaveChanges();
}
Upvotes: 2