lukky replied on Monday, January 05, 2009
Hi,
No, I don't have any Timestamp field, at least not in the application I'm currently working on, which doesn't require concurrency checking. I'm only trying to understand better the use of Linq to SQL.
I don't know if you're familiar with the CSLAContrib CodeSmith templates. My code is based on those.
Here is for example the DataPortal_Update method for a BusinessBase object:
protected override void DataPortal_Update()
{
bool cancel = false;
OnUpdating(ref cancel);
if (cancel) return;
using (var mgr = ContextManager<CentreClient.DAL.vittoria_prodDataContext>
.GetManager(Database.vittoria_prod))
{
var data = new CentreClient.DAL.Customer()
{
CUST_id = ReadProperty<int>(CustIdProperty)
};
mgr.DataContext.Customers.Attach(data);
OnMemberReading(data);
if (IsSelfDirty)
{
data.CUST_NO = ReadProperty<string>(CustNoProperty);
data.CUST_pallet = ReadProperty<int>(CustPalletProperty);
data.CUST_acombacp = ReadProperty<int>(CustAcombacpProperty);
data.CUST_NAME = ReadProperty<string>(CustNameProperty);
data.CUST_shipdelay = ReadProperty<int>(CustShipdelayProperty);
data.CUST_segment = ReadProperty<int>(CustSegmentProperty);
data.CUST_noteclient = ReadProperty<string>(CustNoteclientProperty);
data.CUST_notecommande = ReadProperty<string>(CustNotecommandeProperty);
data.CUST_notefacture = ReadProperty<string>(CustNotefactureProperty);
data.CUST_noteboncommande = ReadProperty<string>(CustNoteboncommandeProperty);
data.CUST_notebilltransport = ReadProperty<string>(CustNotebilltransportProperty);
data.CUST_noteSales = ReadProperty<string>(CustNotesalesProperty);
data.CUST_groupid = ReadProperty<int>(CustGroupidProperty);
data.CUST_groupno = ReadProperty<string>(CustGroupnoProperty);
data.CUST_devise = ReadProperty<string>(CustDeviseProperty);
data.CUST_noteFacPopup = ReadProperty<string>(CustNotefacpopupProperty);
data.CUST_datecreated = ReadProperty<SmartDate>(CustDatecreatedProperty);
data.CUST_dateupdated = ReadProperty<SmartDate>(CustDateupdatedProperty);
data.CUST_taxgroup = ReadProperty<string>(CustTaxgroupProperty);
data.CUST_taxidFed = ReadProperty<string>(CustTaxidfedProperty);
data.CUST_taxidProv = ReadProperty<string>(CustTaxidprovProperty);
data.CUST_termcp = ReadProperty<int>(CustTermcpProperty);
data.CUST_sales = ReadProperty<int>(CustSalesProperty);
data.ACTIVE = ReadProperty<bool>(ActiveProperty);
}
OnMemberRead();
mgr.DataContext.SubmitChanges();
if (IsSelfDirty)
{
LoadProperty<int>(CustIdProperty, data.CUST_id);
}
}//using
OnUpdated();
}
Sorry if this is a bit long.
So, we can see that we create a new entity, and attach it to the DataContext. This code is different than the ProjectTracker sample code, which looks like this:
protected override void DataPortal_Update()
{
using (var ctx = ContextManager<ProjectTracker.DalLinq.PTrackerDataContext>.GetManager(ProjectTracker.DalLinq.Database.PTracker))
{
// insert project data
System.Data.Linq.Binary lastChanged = null;
ctx.DataContext.updateProject(
ReadProperty(IdProperty),
ReadProperty(NameProperty),
ReadProperty(StartedProperty),
ReadProperty(EndedProperty),
ReadProperty(DescriptionProperty),
_timestamp,
ref lastChanged);
_timestamp = lastChanged.ToArray();
// update child objects
FieldManager.UpdateChildren(this);
}
}
Of course, both don't apply to the same database/table, so the fields are different, but we can see the different logic being used.
I'd like to understand what happens with both ways, and why one would chose one over the other. Maybe if you have some link on the subject.
My first impression is that in the first case, we assume that we work with a different DataContext, and need to attach the entity before applying the changes. In the PT sample, we seem to assume that we're using the same DataContext. Is this correct ? If so, does it mean that I need to code differently if I'm going to run the DataPortal on a remote machine than when I run it locally on the user's computer ?
I know I'm asking a lot of question. Blame it on my 3274th gray hair
Regards.