主机提供程序不允许远程访问数据库。覆盖配置中的种子方法.cs不起作用



我的托管提供商(www.binero.se)不允许远程访问他们的数据库,因此很难部署我的web应用程序。我必须将我的MVC应用程序发布到他们的服务器上,只希望它能正常工作。我希望部署的应用程序是在Visual Studio 2013中创建新MVC应用程序时获得的标准应用程序。我使用EnableMigrations-EnableAutomaticMigrations将迁移添加到应用程序我用的是EF6和MVC5。我尝试访问的服务器是MS SQL server。

我需要为AspNet Identity创建表,并最终创建其他一些表,但Configuration.cs中的Initial Migration和Seed方法没有被调用。正如我所理解的,应该在访问时调用它们并创建和填充数据库,这就是我使用迁移的原因。我尝试添加Elmah日志以更好地报告错误,但它似乎只适用于我的本地主机。如果我在抛出异常后尝试访问s/elmah.axd,服务器只会告诉我资源不存在。Wtf?我以为埃尔玛会负责路线。

有趣的是,如果我不使用迁移和种子方法,ASP确实会创建数据库表。换句话说:如果我只是发布标准模板MVC应用程序并尝试注册,它会在数据库中创建表并插入新注册的用户数据。这告诉我们连接字符串没有任何问题,对吧?那么,IdentityDbContext怎么可能创建表并插入数据,但当我尝试使用ApplicationContext(从IdentityDbContext派生)通过Configuration.cs进行种子时,它不起作用。没有创建任何表,也没有插入任何数据。为什么?我已经扯了24小时的头发,想弄清楚这一点。请帮帮我。

不管怎样,当我进入账户/登录并输入我的凭据时,它会显示:

"已为上下文"ApplicationDbContext"启用迁移,但数据库不存在或不包含映射表。使用迁移要终止数据库及其表,例如通过运行包管理器控制台中的"更新数据库"命令"

我知道它不存在!这就是为什么我需要您运行Initial_Create迁移

我也试过并设法得到一个内部服务器错误(500),但我无法调试发生的事情。

配置.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>

这是一个老话题,但我会尝试解释一些东西。

Binero目前不支持迁移的完全扩展。"调试"模式下的代码优先迁移会擦除数据库,并创建一个以模型为参考的新数据库。Binero不允许这样做,因为您无法擦除和创建数据库(这是在Binero控制面板中完成的)。相反,您需要告诉实体框架删除所有表,并在与数据库的当前连接上播种新表。

这是一个链接,解释了如何:http://www.binero.se/support/faq/webbdatabas/aspnet/hur-anvander-jag-aspnet-mvc3-ef-41-codefirst

还有其他问题,Binero不支持MVC5和实体框架更新到4.1版本(来自他们网站上的内容http://www.binero.se/support/faq/webbdatabas/aspnet/vilka-komponenter-har-ni-installerade-i-er-windowsmiljo)

更新:即使是强硬的Binero也没有说他们支持MVC5或EF6,我发现如果你把域设置为.Net Framework 4.5.1(或4.5,我不记得哪一个是有效的替代方案),你确实可以用EF6发布MVC 5。

我自己也做过几次,只有Asp.Net Identity 1.0.0版本很难,所以我不能保证支持更新的版本。

最新更新