Home

LINQPaq Querying UserQuery DB And Not Correct DB

This is a cross-post from a SO question. Since it doesn't seem to be drawing any attention, I figured I would ask here.

I have created a connection in LINQPad that uses a project's EfDBContext. After some recent issues I can now get it to query a table, but when it does it created a whole new schema.

So, when creating the connection (Entity Framework POCO Connection), when I click "Test" the Profiler shows me:

SELECT Count(*) FROM sys.databases WHERE [name]=N'mydatabase'

But when I run Ethnicities.Take (100) I see:

SELECT Count(*) FROM sys.databases WHERE [name]=N'UserQuery'

Followed by all the commands to generate the schema. What am I doing wrong or need to change to get it to read the data in mydatabase?

Comments

  • Update: After more messing around thinking my code was off, I noticed LINQPad will not only query for the mydatabase database on testing, but it will also query for a list of tables from mydatabase when I initially expand the connection to show the tables. Immediately when I go to run Ethnicities.Take (100) is when LINQPad attempts to create an entirely new database. It is now throwing FK errors because I don't have the code set up the way it likes it. EfDBContext works perfectly fine (fetches all related data as expected) when ran normally.
  • I found this:

    http://forums.oreilly.com/topic/49460-linqpad-entity-framework-41-poco-query-problem/

    And adding this line of code in my DbContext file fixed it:

    public EfDbContext() : base("mydatabase") { }

    Unsure if this is still a known bug or an issue with me, since I tried the beta and still had the same issue.
  • edited June 2013
    LINQPad normally avoid this problem by copying and patching your application configuration file. (Perhaps you don't have an app.config file, in which case it will fail?)

    You can verify it by running the following query:
    
    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Dump("app.config location");
    File.ReadAllText (AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).Dump("contents");
    The location of your config should be a temporary file in %temp%\LINQPad. The content of the configuration file should contain an element, under connectionStrings with add name="UserQuery"...
  • edited June 2013
    Thanks, Joe!

    I tried that, and still got the same issue. I wrote my App.config as so:

    <configuration> <connectionStrings> <add name="EFDbContext" connectionString="Data Source=.\SqlExpress;Initial Catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
    And after removing my previous fix LINQPad still tried to create the UserQuery database. I also ran your provided code and got:

    app.config location C:\Users\JJ\Documents\Visual Studio 2012\Projects\RMC\RMC.Backend\App.config contents <configuration> <connectionStrings> <add name="EFDbContext" connectionString="Data Source=.\SqlExpress;Initial Catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
    Am I doing anything wrong here?
  • edited July 2013
    I also had this same problem. I should also mention that I'm using the parameter-less constructor, I'm not pointing to an app.config file, and my code-first model is completely convention driven. If LINQPad is simply instantiating my DbContext, it should find the database automatically... I don't even have a constructor defined in my DbContext!

    I solved it by adding a parameter-less constructor that calls the base constructor with the full name of my DbContext type as a parameter.

    Joe, you wrote back in 2011 at the O'Reilly forums:
    Because LINQPad subclasses your typed DC (so you can just type 'Customers' when querying), the virtual GetType() method returns the subclassed type ("UserQuery") instead. I've fixed this by having LINQPad copy your app.config file to a temp directory, and modify the config so that it also contains a matching entry for 'UserQuery'.
    I'm guessing that due to how the EF constructor is written, there is no solution other than the one that worked for Codeacula and myself?
Sign In or Register to comment.