Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build-Solution.ps1 only works with .\SQLEXPRESS instance #221

Closed
yeurch opened this issue Nov 14, 2011 · 12 comments
Closed

Build-Solution.ps1 only works with .\SQLEXPRESS instance #221

yeurch opened this issue Nov 14, 2011 · 12 comments
Assignees

Comments

@yeurch
Copy link

yeurch commented Nov 14, 2011

Regardless of any changes made to the connection string in Website\Web.config, I've found that the Build-Solution.ps1 PowerShell script always tries to connect to the local SQLEXPRESS instance of SQL Server when running UpdateDatabase, causing a build failure if the instance doesn't exist.

Building and running the solution in Visual Studio works fine for me, and creates and initializes the database correctly.

@yeurch
Copy link
Author

yeurch commented Nov 29, 2011

Had a chance to get further info on this. The problem is because the connection string obtained in the Powershell script is not passed to the UpdateDatabase MSBuild task. Instead, the MigrationsContext class passes the string "NuGetGallery" to its base class constructor (line 51 in Settings.cs)

public class MigrationsContext : EntitiesContext
{
    public MigrationsContext()
        : base("NuGetGallery")
    {
    }
}

Because the build script doesn't have an associated configuration file (App.config or Web.config), then the connection string with name "NuGetGallery" can't be found, leaving Entity Framework to fall back to its default behaviour of using the .\SQLEXPRESS database instance (which doesn't exist on my machine).

I'm not sure of any way to get a full connection string to Entity Framework using Code First. I tried a test by replacing the literal string "NuGetGallery" with the actual connection string of my database, but the error is then System.Configuration.ConfigurationErrorsException: Invalid key value. I think that is because Entity Framework expects an EntityFramework connection string, not a traditional System.Data.SqlClient one.

If anyone has any ideas how to progress this, I'd be glad to help out.

@half-ogre
Copy link
Contributor

@yeurch As soon as we get through the 1.6 release, I'll look into this.

@yeurch
Copy link
Author

yeurch commented Nov 30, 2011

@anglicangeek Thanks Drew, I tried to play around a little more by passing a DbConnection to the DbContext constructor, instead of a string. To do this, I did the following:

--- a/Website/Entities/EntitiesContext.cs
+++ b/Website/Entities/EntitiesContext.cs
@@ -15,8 +15,8 @@ namespace NuGetGallery
         {
         }

-        public EntitiesContext(string connectionStringName)
-            : base(connectionStringName)
+        public EntitiesContext(string connectionStringOrName)
+            : base(GetConnection(connectionStringOrName, useProfiling: false), contextOwnsConnection: true)
         {
         }

@@ -110,10 +110,12 @@ namespace NuGetGallery
             set;
         }

-        private static DbConnection GetConnection(string connectionStringName)
+        private static DbConnection GetConnection(string connectionStringOrName, bool useProfiling = true)
         {
-            var setting = ConfigurationManager.ConnectionStrings[connectionStringName];
-            var connection = new SqlConnection(setting.ConnectionString);
+            var setting = ConfigurationManager.ConnectionStrings[connectionStringOrName];
+            var connection = new SqlConnection(setting == null ? connectionStringOrName : setting.ConnectionString);
+            if (!useProfiling)
+                return connection;
             return ProfiledDbConnection.Get(connection);
         }
     }

Then, to test this approach, I replaced "NuGetGallery" with my actual Sql connection string, as detailed above. Still get the invalid key value exception.

@kamranayub
Copy link

I think the issue lies in System.Data.Entity.Migrations.DbMigrationContext in the CreateContextInfo:

 string connectionStringName = this.ConnectionStringName;
    string connectionString = this.ConnectionString;
    bool flag = string.IsNullOrWhiteSpace(connectionString);
    string connectionProviderName = this.ConnectionProviderName;
    bool flag2 = string.IsNullOrWhiteSpace(connectionProviderName);
    if ((flag || flag2) || string.IsNullOrWhiteSpace(connectionStringName))
    {
        DbContextInfo info = new DbContextInfo(this.ContextType);
        connectionStringName = info.ConnectionStringName;
        if (flag)
        {
            connectionString = info.ConnectionString;
        }
        if (flag2)
        {
            connectionProviderName = info.ConnectionProviderName;
        }
    }
    ConnectionStringSettingsCollection connectionStringSettings = new ConnectionStringSettingsCollection();
    connectionStringSettings.Add(new ConnectionStringSettings(connectionStringName, connectionString, connectionProviderName));
    return new DbContextInfo(this.ContextType, connectionStringSettings);

By all rights, I think it is getting the literal string you're using, but it seems to be trying to add a connection string to a collection? Perhaps this is why the stack trace shows:

System.Configuration.ConfigurationErrorsException: Invalid key value.
  at System.Configuration.ConfigurationElementCollection.GetElementKeyInternal(ConfigurationElement element)
  at System.Configuration.ConfigurationElementCollection.BaseAdd(ConfigurationElement element, Boolean throwIfExists, Boolean ignoreLocks)
  at System.Configuration.ConfigurationElementCollection.BaseAdd(ConfigurationElement element)

At any rate, this is super annoying because my local instance is not named SQLEXPRESS.

@odelljl
Copy link

odelljl commented Feb 9, 2012

Is there a known workaround in the mean time?

@half-ogre
Copy link
Contributor

I don't know, because I haven't looked at the problem yet. I can look at this today or tomorrow.

@odelljl
Copy link

odelljl commented Feb 9, 2012

Thanks - we are considering working with the project for in house package management and I wanted to give a demo. If I can assist let me know - I'm just familiarizing myself with the project.

@yeurch
Copy link
Author

yeurch commented Feb 15, 2012

@odelljl Don't let this issue stop you from working with the project. The solution builds just fine inside Visual Studio and seems to work with no issues. We're using this for in house package management ourselves.

@half-ogre
Copy link
Contributor

Yep, like @yeurch said, please don't let this stop you from playing with the source. I've been dreadfully sick the last few days, but this is high on my list of things to look at.

@odelljl
Copy link

odelljl commented Feb 15, 2012

@yeurch Yes - thanks! - I just noticed it was EF Code First. Quick change of the connection string and I'm up and running.

@half-ogre
Copy link
Contributor

It looks like this will be fixed when we upgrade to EF 4.3. I can't do that just yet because I can't get MiniProfiler to work with it. In the mean-time, I think I'm going to just yank the update and revert scripts out, since EF 4.3 will have it's commands for this anyway, and there is a app_start update in there already, too.

@odelljl
Copy link

odelljl commented Feb 20, 2012

Thanks - that makes sense to me.

@ghost ghost assigned half-ogre Feb 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants