Hi,
I need to insert or update a number of price codes in the database. I would prefer to do this in one session for performance reasons but I am running into trouble when a price code appears twice in the input feed, either with exactly the same info or with changed info. If the info is the same I would like to just ignore the input, if the info is different I want to update the record. Point is, the record might have been created earlier in the session, so my SELECT statement returns that there is no record yet. So I create a new row, try to Save it and get a UniqueKeyViolation exception (or similar).
So right now I am Flushing the session after each SaveOrUpdateCopy (I use *Copy to prevent yet another related issue). I would most definately like to prevent having to Flush every time.
Is there any way I can do my SELECT statement (or a similar construct) on the session itself, so I can retrieve a record that was cerated earlier in the session? I've seen the Get method but that does not allow me to specify a query.
My code is below:
Code:
for ( ; current < priceCodes.Count; current++ )
{
XmlNode priceCode = priceCodes[current];
// The following 4 values uniquely identify a price code
string resourceCode = priceCode["RsrcCode"].InnerText;
string countryCode = priceCode["CtyCodeRel"].InnerText;
string dspCode = priceCode["CpyIDSP"].InnerText;
DateTime effectiveDate = DateTime.ParseExact( priceCode["EffectiveDate"].InnerText, "yyyy-MM-dd", null );
PriceCodeService pcs = new PriceCodeService();
ReleasePriceCode pc = pcs.GetPriceCode( resourceCode, countryCode,
dspCode, effectiveDate );
if ( pc == null )
{
Log.DebugFormat( "Price Code ({0}:{1}:{2}) doesn't exist, need to create",
resourceCode, countryCode, dspCode );
pc = new ReleasePriceCode();
}
pc.ResourceCode = resourceCode;
pc.CountryCode = countryCode;
pc.DigitalSalesPartnerCode = dspCode;
pc.EffectiveDate = effectiveDate;
pc.PriceCode = priceCode["PriceCode"].InnerText;
// *Copy is required to enable updating an existing record that was previously updated
session.SaveOrUpdateCopy( pc );
// Flush is required for GetPriceCode (above) to return a previously created price code
session.Flush();
}
I hope I've made my problem clear enough and that someone can provide some assistance.
Thanks!
Erik