实体框架如何更新记录

时间:2014-11-06 18:33:54

标签: c# entity-framework entity-framework-6 ado.net-entity-data-model

我正在尝试创建一个简单的应用程序,我在其中查询实体,获取结果,通过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 ?


          }
        }
    }
  }
}

2 个答案:

答案 0 :(得分:2)

当我正确阅读你的代码时,我正在做一些假设:

  • VertextGeoCode是一个字符串
  • 结果(reslt)必须直接放在VertextGeoCode中而不做任何修改。

不要在查询中创建匿名类型,只需选择实体本身:

var query = from b in db.SalesOrder_FromSF
            where b.VertexGeoCode.Length==1
            select b;

将您的webservice对象直接分配给实体属性并推回结果。然后只需在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();
}

答案 1 :(得分:0)

我通过使用

找到了另一种方法
using (TransactionScope scope = new TransactionScope())

{
using ( var db= new dbEntity)
{
//query code 
 Foreach ( var item in query)
{
 //program logic 
 db.savechange();
}
}
scope.complete();
}

通过上面的操作我能够在foreach循环中进行保存并保存我需要的转换以确保有主键并且还需要引用system.Transctions

相关问题