The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Roger Meddows Taylor (1) [Avatar] Offline
#1
Fabrice, thans a lot for your devotion and for your "tasty" book. It moved me to go and try LINQ in my projects. But as soon as I started - questions began to arise.

Here's one of 'em:

Ive got strange problem with LINQ - to be more precise with AutoSync pattern.

I'm currently building a simple import-export service - objects are queried from one place (paradox file database), some business logic gets applied and then we store them to SQL Server.

So the problem is - when I get an initial IEnumerable<BusinessObject> via query against connection to Paradox - I populate it to my Sql Server data context through corresponding Table<BusinessObject>.InsertAllOnSubmit(IEnumerable<BusinessObject>smilie method.

Then I call dataContext.SubmitChanges() (obviously).

As soon as I have autoincremental identity as PK of BusinessObject entity, I've specified autosync behavior like 'IsDbGenerated = true,AutoSync=AutoSync.OnInsert'.

So when I try to get new autosynced pk values from initial query IEnumerable<BusinessObject> - i don't get it, I mean objects are not synchronized.

I spent some time on it and found, that good synchronised objects are only available through Table<BusinessObject>.

And the question is - am I right, assuming that LINQ clones all objects from query that stands agains paradox to some new storage, that's associated with SQL connection and only this cloned ones are synchronised through AutoSync pattern ???

here's some code

//// Query objects from paradox
oleConnection.Open();
oleDbDataAdapter.Fill(hozopTable);
oleConnection.Close();

var operationQuery = from hozOper in hozopTable.AsEnumerable()
select new ibOperation()
{
ibBaseID = 1,
ibOperationDate = hozOper.Field<DateTime>("MDATE"),
ibOperationSum = Convert.ToDecimal(hozOper.Field<Double>("ALLSUM")),
ibOperationNum = hozOper.Field<int>("OPER_NUMB"),
ibOperationSaveDate = DateTime.Now
};

//// Create context associated with SQL Server
SqlConnection pakDBConn = new SqlConnection(pakDBConnStr);
PakDbDataContext dataContext = new PakDbDataContext(pakDBConn);
Table<ibOperation> operationTable = dataContext.DataContext.GetTable<ibOperation >();
//// Insert and submit
operationTable.InsertAllOnSubmit(operationQuery);
dataContext.DataContext.SubmitChanges();

/// checkout what we've got in id property

int idFromQuery = operationQuery.ToList()[0].ibOperationID; // ==0 here and everywhere through Enumeration

int idFromTable = operationTable.ToList()[0].ibOperationID; // == [correct identity value] here and everywhere in Table

So - Fabrice - I'd be much oblidged for clarifying the situation !