Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Sunday, May 3, 2009

LINQ and ConnectionString

There are some issues you should know when you are working with Database Markup Language(DBML) or generating LINQ to SQL classes in VS.2008 IDE:

Why is the dropdown box of "Choose your context object" always empty or why can't I find my context object while I am configuring LinqDataSource?

Why do I have this error "No parameterless constructor defined for this object"?

Why does the actual connection string suddenly appear inside the parameterless contructor of my DataContext class?

Why can't I add a database table to DBML / LINQ designer?



  1. Why is the dropdown box of "Choose your context object" always empty or why can't I find my context object while I am configuring LinqDataSource?

    Please be sure the DLL containing the context object existed and you have a reference to it. If the LINQ class is newly created, please compile the project first before you try to configure LinqDataSource.

  2. Why do I have this error "No parameterless constructor defined for this object"?

    If you remove the ConnectionString manually from the Connection element of the DBML source file first (use external text editor to remove it) and then set the Application Settings to False (also see How to Turn off Application Settings for LINQ), your parameterless constructor of the DataContext class (e.g., Book.designer.cs) will immediately be removed by the IDE. Then the above error will occur when you recompile and run the application.

    Normally enabling Application Settings on LINQ will have the parameterless constructor generated for you automatically, as long as its ConnectionString property is not empty. Indeed, you won't be able to remove the ConnectionString property inside the IDE.  But you remove it anyway outside it. When this happens, the original parameterless contructor will lose forever regardless of Application Settings. Thereafer, adding back the ConnectionString property to the file will not resolve it. And the other problem dicussed in the next question may occur.

    To resolve this, you need to manually add the original parameterless constructor back.

  3. Why does the actual connection string (like below) suddenly appear inside the parameterless contructor of my DataContext class? For example,
        public BookDataContext() :
            base("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Books.mdf;Integrated Security=True;User Instance=True", mappingSource)
        {
            OnCreated();
        }

    Situation 1:    If you set the Application Settings to False while the ConnectionString property exists in the DBML source file, your original parameterless constructor of that DataContext class will be altered by hard-coding with the actual connection string that can be found in your .config file (e.g., web.config).

    Situation 2:    The application had Application Settings set to False and the correct default parameterless constructor some time ago. Later, you make a change by adding a new table to the LINQ. As soon as you drag the new table to the DBML or LINQ designer, your original, correct parameterless contructor will be altered by hard-coding with the actual connection string.

    Currently, I don't know any other easy way to deal with it. If you know, please jot me a note. What I do is

    1. First, ensure Application Settings for LINQ is set to False.
    2. Then, manually alter or copy the parameterless contructor back to the original
    3. .

  4. Why can't I add a database table to DBML / LINQ designer?

    This happens when

    • your Application Settings for LINQ is set to False,
    • ConnectionString element doesn't exist in the DBML source file, and
    • the parameterless constructor in LINQ class is missing.

    The IDE needs to know where to read the connection string in order to access the database. Please ensure you have the parameterless contructor defined correctly while remaining Application Settings for LINQ to False and the absence of ConnectionString in the DBML source file.

Sample of Parameterless Constructor in a ContextClass

public BookDataContext() :     base(global::System.Configuration.ConfigurationManager.ConnectionStrings["BookConnectionString"].ConnectionString, mappingSource) {     OnCreated(); } where BookDataContext is my DataContext class, BookConnectionString is the name of the connection string found in .config file. You should change them to fit yours accordingly.

How to Turn off Application Settings for LINQ
The default setting of LINQ to SQL class is always True. You can do the following to disable it.

On VS IDE
  • Go and double-click to open the DBML file from Solution Explorer.
  • Then open the file Properties window if it is not open. Be sure that the focus is on the DBML designer tab; otherwise, you won't see or cannot continue with the next step.
  • Expand Connection and then set Application Settings to False.