简体   繁体   中英

how to refrence Linq2SQL class from a static class

i am having 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 in my page i do the following

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

the record get inserted but with null value of the ArticleTitle field.

Each time you're accessing BlogDataAccess.Blg , you're creating a new object. I think you mean to implement the lazy-instantiation like this instead:

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

            return _Blg;
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM