开发者

C# Linq-to-SQL Not Updating After Ado.net Insert

I've created a 1:many relationship and bulk inserts I make via ADO.net aren't propagating to my LINQ-to-SQL data context.

e.g.

TABLE School
  int schoolId
  int maxStudents
  ...

TABLE Students
  int studentId
  int schoolId
  DateTime birthdate
  ...

If I bulk add, say, 500 students to the Students table, my data context doesn't update .Students. 开发者_如何学PythonIt just says .Students.Count() == 0.

EDIT Here's an example:

// Create a Repository
Repository Repo = new Repository();
string[] schoolNames = GetSchoolNames();

// Pull in the schools.
IEnumerable<School> schools = Repo.GetSchools(schoolNames);
foreach (School school in schools) school.DoSomeDirectDBWork();
schoolNames = schools.Select(school => school.name).ToArray();
Repo.Dispose();  // Flush/Close/Dispose

Repo = new Repository();  // New repository, new data context.
schools = Repo.GetSchools(schoolNames);
// ** THE SCHOOLS HAVE NO STUDENTS AT THIS POINT IN THE CODE.
// ** <SAD FACE>


I wonder if this is simply cacheing. If you have forced it to load an unfiltered set of students, then sure - it might well the trust that set (empty at the point of load). DataContext is intended to be short-lived; try creating a new one after the insert and retry. If this hasn't already read the empty table it should find your data.

Additionally, check you don't have two Students tables - for example dbo.Students vs bluesummers.Students.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜