Dennis Dyallo
Dennis Dyallo

Reputation: 475

Host provider does not allow remote access to database. Overriding the Seed-method in Configuration.cs doesn't work

My hosting provider (www.binero.se) does not allow remote access to their databases, making it hard to deploy my web app. I have to publish my MVC App to their servers and just hope that it works. The app that I wish to deploy is the standard app you get when creating a new MVC App in Visual Studio 2013. I added Migrations to the App using Enable-Migrations -EnableAutomaticMigrations I'm using EF6 and MVC5. The server I'm trying to access is a MS SQL Server.

I need to create the tables for AspNet Identity and eventually a few other tables, but the Initial Migration and Seed-method in Configuration.cs aren't being called. As I've understood, they should be called and create and populate the database upon access, that's why I'm using the Migrations. I tried adding Elmah logging for better error reporting, but it only seems to work on my localhost. If I try to accesss /elmah.axd after an exception has been thrown, the server just tells me that the resource doesn't exist. Wtf? I thought Elmah would take care of the routing.

The funny thing is, that ASP DOES create the Database tables if I don't use Migrations and a Seed method. In other words: if I just publish the standard template MVC app and try to register, it creates the tables in the database and inserts the newly registered user data. That tells us that there is nothing wrong with the connection string, right? So how is it possible that IdentityDbContext can create the tables and insert data, but when I try to Seed through the Configuration.cs using the ApplicationContext (which derives from IdentityDbContext) it doesn't work. No tables are created, no data is inserted. Why?! I've been ripping my hair our for 24 hours trying to figure this out. Please help me.

Anyway, when I go to Account/Login and enter my credentials, it says:

"Migrations is enabled for context 'ApplicationDbContext' but the database does not exist or contains no mapped tables. Use Migrations to ceate the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console."

I know it doesn't exist! That's why I need you to run the Initial_Create Migration!

I have also poked around and managed to get an Internal Server Error (500), but I cannot debug what happens.

Configuration.cs

 internal sealed class Configuration : DbMigrationsConfiguration<UsersSeedMigrationCSharp.Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(UsersSeedMigrationCSharp.Models.ApplicationDbContext context)
    {
        var UserManager = new UserManager<ApplicationUser>(new
                    UserStore<ApplicationUser>(new ApplicationDbContext()));
        var RoleManager = new RoleManager<IdentityRole>(new
                            RoleStore<IdentityRole>(new ApplicationDbContext()));
        string password = "password";
        //Create Role Admin if it does not exist
        if (!RoleManager.RoleExists("admin"))
        {
            RoleManager.Create(new IdentityRole("admin"));
        }


        var user = new ApplicationUser { UserName = "admin" };
        var addresult = UserManager.Create(user, password);
        //Add User Admin to Role Admin
        if (addresult.Succeeded)
        {
            var result = UserManager.AddToRole(user.Id, "admin");
        }

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup></configSections>
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-UsersSeedMigrationCSharp-20131217074025.mdf;Initial Catalog=aspnet-UsersSeedMigrationCSharp-20131217074025;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="DefaultConnection" connectionString="Data Source=XXX;Initial Catalog=XXXXX;Persist Security Info=True;User ID=XXX;Password=XXX" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<customErrors mode="Off"/>
<httpRuntime targetFramework="4.5" />
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules></system.web>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /><add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /><add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /></modules>
<validation validateIntegratedModeConfiguration="false" /></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<elmah>
<!--
See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
more information on remote access and securing ELMAH.
-->
<security allowRemoteAccess="true" />
</elmah><location path="elmah.axd" inheritInChildApplications="false">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<!-- 
See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
more information on using ASP.NET authorization securing ELMAH.

<authorization>
<allow roles="admin" />
<deny users="*" />  
</authorization>
-->  
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location></configuration>

Upvotes: 0

Views: 580

Answers (1)

J.Olsson
J.Olsson

Reputation: 815

This is a old topic but i will try to explain somethings.

Binero currently don't support the fully extend of Migrations. The code first migrations in the "debug" mode erases the database and create a new one with the Models as reference. Binero don't allow this, because you can't erase and create your database(that is done within the binero control panel). Instead you need to tell the Entity Framwork to delete all the tables and seed new tables on the current connection to the database.

This is a a link that explains how: http://www.binero.se/support/faq/webbdatabas/aspnet/hur-anvander-jag-aspnet-mvc3-ef-41-codefirst

And the other problems, Binero don't support MVC5 and Entity Framework newer than version.4.1 (from what i read on their website http://www.binero.se/support/faq/webbdatabas/aspnet/vilka-komponenter-har-ni-installerade-i-er-windowsmiljo)

Update: Even tough Binero dont say they support MVC5 or EF 6, I've discovered that if you setup the domain as .Net Framework 4.5.1(or 4.5 I don't remember which one is the valid alternative) you can indeed publish a MVC 5 with EF6.

I've done it myself a couple of times, tough only with Asp.Net Identity Version 1.0.0, so i cant guarantee support for a newer version.

Upvotes: 2

Related Questions