RE: LINQ to SQL: OnLoaded() not firing
- From: v-micsun@xxxxxxxxxxxxxxxxxxxx (Lingzhi Sun [MSFT])
- Date: Tue, 14 Apr 2009 05:02:17 GMT
Hi Mark,
OnLoaded is one of the extension methods of the LINQ to SQL entity classes,
which is similar to the other methods like OnCreated, OnValidate, and etc.
The OnCreated method is called when the entity class is created. We can
see this method is inside the entity class's constructor.
===========================
// LINQ to SQL entity class Person
public Person()
{
// Other code logic.
OnCreated();
}
===========================
The OnLoaded method is called after the entity class has been loaded from
the database. The OnValidate method is called before the entity class is
saved into the database (when we call the DataContext.SubmitChanges
method). All the above three methods need to be implemented by ourselves
according to the certain code logic. For detail, please see
http://msdn.microsoft.com/en-us/library/bb882671.aspx.
When we generate a LINQ to SQL data query, for performance reasons, the
entity classes are not created and loaded locally until we try to retrieve
the data inside the query result. For detail, please see the following
code snippet and the comments:
===========================
// The extension methods implementation
// of the entity class Person
public partial class Person
{
partial void OnLoaded()
{
Console.WriteLine("Person {0} {1} is loaded...",
this.FirstName, this.LastName);
}
partial void OnCreated()
{
Console.WriteLine("New person is created...",
this.FirstName, this.LastName);
}
}
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
// Create the DataContext object.
DataClasses1DataContext db =
new DataClasses1DataContext();
// Make the query to get the person whose first
// name is Roger.
// After the query is evaluated, the OnCreated
// and OnLoaded methods are not fired.
var query = from p in db.Persons
where p.FirstName == "Roger"
select p;
// Output each person's name
// Here the corresponding OnCreated and OnLoaded
// methods will be fired.
foreach (var q in query)
Console.WriteLine("{0} {1}", q.FirstName,
q.LastName);
// To bind the query result to some data control
// will also fire the OnCreated and OnLoaded methods.
dataGridView1.DataSource = query;
}
===========================
The output is like:
New person is created...
Person Roger Harui is loaded...
Roger Harui
......
===========================
If you have any further questions regarding this case, please be free to
let me know.
Regards,
Lingzhi Sun (v-micsun@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- References:
- LINQ to SQL: OnLoaded() not firing
- From: Mark Olbert
- LINQ to SQL: OnLoaded() not firing
- Prev by Date: Re: Luddites ! ; forward into the past ! (ADO/DAO)
- Next by Date: RE: OdbcConnection.GetSchema("tables") all wrong for .dbf file
- Previous by thread: LINQ to SQL: OnLoaded() not firing
- Next by thread: RE: LINQ to SQL: OnLoaded() not firing
- Index(es):