LINQ to SQL Caching Problem

it should be pretty simple.

My LINQ to SQL (C # 3.5 / SQL Server) works fine for me, with a simple relationship between the two Conference tables and ConferenceAttendees .

I already created and completed the Conference , then I add 3 ConferenceAttendees with the following code (repeated 3 times):

ConferenceAttendee NewAttendee = new ConferenceAttendee();
NewAttendee.ConferenceId = ConferenceID;
NewAttendee.Name = ContactName;
NewAttendee.Email = ContactEmail;
db.ConferenceAttendees.InsertOnSubmit(NewAttendee);
db.SubmitChanges();

Works great, and I see that 3 new members appear in the database.

UPDATE: Then, using the new datacontext, I try the following:

string Names = String.Empty;
Conference RequestedConference = db.Conferences.FirstOrDefault(x => x.ConferenceId == ConferenceID);
   foreach (ConferenceAttendee Attendee in RequestedConference.ConferenceAttendees)
      Names += Attendee.Name + ", ";

But he has no Attendees attached to him! (They definitely exist in the database and were committed). But RequestedConference.ConferenceAttendees always has a score of 0, so the loop is never entered.

foreach (ConferenceAttendee Attendee in this.ConferenceAttendees)
{ Names += Attendee.Name; }

Partial Class, , PrintAllAttendeeNames().

, datacontext LINQ, ?

(:

db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

..)

. dataContext . , .

:

public static MyDataContext db = new MyDataContext(ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString);

, , !!!

public MyDataContext db = new MyDataContext(ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString);
+3
1

. -, . SubmitChanges , . , InsertOnSubmit , SubmitChanges. LINQ to SQL , .

-, . Attendee. , , . L2S , . DataLoadOptions:

using (var context = new SomeContext(...))
{
    var options = new DataLoadOptions();
    options.LoadWith<Conference>(c => c.ConferenceAttendees);

    context.LoadOptions = options;

    var conferencesWithAttendees = from c in context.Conferences
                                   where c.Year = DateTime.Now.Year
                                   select c;

    foreach (var conferenceWithAttendee in conferencesWithAttendees)
    {
        conferenceWithAttendee.PrintAllAttendeeNames();
    }
}
+1
source

Source: https://habr.com/ru/post/1763019/


All Articles