[英]Entity Framework how to update a record
我正在嘗試創建一個簡單的應用程序,用於查詢實體,獲取結果,通過Web服務運行結果並獲取缺少的信息。 我一直堅持用新的和更新的結果來更新數據庫,這里是我到目前為止的代碼。
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 ?
}
}
}
}
}
當我正確閱讀您的代碼時,我正在做一些假設:
無需在查詢中創建匿名類型,只需選擇實體本身即可:
var query = from b in db.SalesOrder_FromSF
where b.VertexGeoCode.Length==1
select b;
將您的Web服務對象直接分配給實體屬性,然后推回結果。 然后只需在dbContext上調用SaveChanges()
即可:
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();
}
我找到了另一種方法
using (TransactionScope scope = new TransactionScope())
{
using ( var db= new dbEntity)
{
//query code
Foreach ( var item in query)
{
//program logic
db.savechange();
}
}
scope.complete();
}
通過以上操作,我能夠在foreach循環中進行保存並保存需要確保具有主鍵並且還需要引用系統的轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.