LINQ to SQL, DataContext issue
While using LINQ to SQL, I created a static DataContext in my custom class i.e.
public class OPIS
{
public static DataContext DataContext = new DataContext();
....
}
Now whenever i need to query (including insert and delete) results via LINQ i use this static DataContext object. I never figured out a problem until I was unable to get the the recently inserted object in one of the EntitySet e.g.
I have a Profile
and a BasicInfo
EntityType with one-to-one relationship with eachother.
//Inserting a new BasicInfo Object, have created association with Profile object before
OPIS.DataContext.BasicInfos.InsertOnSubmit(basicInfoObject);
Now on another page
Profile profile = //get new profile from linq, comming fine
BasicInfo bi = profile.BasicInfo //returning null, it shouldn't
bi
should return an object of associated Bas开发者_开发问答icInfo with profile, but it is not. Why?
Are you ever calling SubmitChanges()
on your DataContext
? If not, then all you are doing is registering the BasicInfo
for insertion, but never inserting it.
OPIS.DataContext.BasicInfos.InsertOnSubmit(basicInfoObject); //registers object for insertion
OPIS.DataContext.SubmitChanges(); // performs insert
精彩评论