none
WCF and LINQ DataContext Issues RRS feed

  • Question

  • Hi!

    I ran into a problem using Linq in my WCF service (built with VS 2013 & .NET ver 4.5). Normally when connecting to a database I use linq-to-sql classes as they simplify so many things yet in a recent WCF service I tried using a linq-to-sql class and run into an exception. Te exception is thrown on calling the DataContext constructor that is auto generated when you drag/drop a table onto the .dbml.

    The db connection to VS2013 is fine and the connection string in my web.config is correct so I'm at a loss.

    I can connect to the db using the older SqlConnectionapproach as follows and all works fine.

    using (SqlConnection SqlConn = new SqlConnection(connectionString))

    {

      SqlCommand sqlCmd = new SqlCommand("Select * From TragetTables);

    }

    but using the DataContext generated for the dbml throws an "object not set to an instance" exception when the constructor is called.

    TargetDataContext ctx = null;

    using (ctx = new TargetDataContext ())

    {

        var data = ctx.TragetData;

    }

    It seems that the connection string in the web.config is partially responsible for throwing the exception. That is, I tried making a copy of the connection string

    string conString =

    System.Configuration.ConfigurationManager.ConnectionStrings["TargetDataConnectionString"].ConnectionString;

    Would anyone know what the problem is?

    I thought maybe I am missing some settings for wcf such as binding?

    Thanks!

    Tuesday, May 26, 2015 5:48 AM

All replies

  • Hi JDKOA,

    Since this issue is related to the LINQ, I help you move this case to the LINQ to SQL forum for dedicated support.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 27, 2015 6:48 AM
  • Hello JDKOA,

    >> but using the DataContext generated for the dbml throws an "object not set to an instance" exception when the constructor is called…

    Which line throws this exception exactly, I am confused because I created a similar test while it works fine:

    public void GetData()
    
            {
    
                using (DataClasses1DataContext db=new DataClasses1DataContext())
    
                {
    
                    var result = db.Sites.ToList();
    
                }
    
            }
    

    >> That is, I tried making a copy of the connection string

    If you copied the connection string, does it work for you?

    >> I thought maybe I am missing some settings for wcf such as binding?

    Since it is not clear what your table is and what your exact steps to create the WCF project, it is hard to tell which part causes this issue, I suggest you could create a console app with the same database to check if it works.

    Regadrs.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 27, 2015 7:28 AM
    Moderator