How to drop Linq2SQL class from static class

I have a strange problem, I have a static class like this

  public static class BlogDataAccess
{
    private static Blog _Blg;
    public static Blog Blg
    {
        get
        {
           _Blg = new Blog ();
           return _Blog ;
        }
    }

}

      

then on my page i will do the following

  var DataContext= new DataClasses();
  BlogDataAccess.Blg.ArticleTitle ="Title";
  DataContext.Blog.InsertOnSubmit(BlogDataAccess.Blg);
  DataContext.SubmitChanges();

      

a record is inserted, but with a null value for the ArticleTitle field.

0


a source to share


1 answer


Each time you access BlogDataAccess.Blg

, you create a new object. I think you want to implement lazy-instantiation instead:



public static class BlogDataAccess
{
    private static Blog _Blg;
    public static Blog Blg
    {
        get
        {
            if(_Blg == null)
                _Blg = new Blog();

            return _Blg;
        }
    }

}

      

+4


a source







All Articles