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

时间:2013-12-17 07:55:39

标签: c# asp.net-mvc-5 entity-framework-6

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

我需要为AspNet Identity创建表,最后创建一些其他表,但是没有调用Configuration.cs中的Initial Migration和Seed-method。 据我所知,应该在访问时调用它们并创建和填充数据库,这就是我使用迁移的原因。我尝试添加Elmah日志记录以获得更好的错误报告,但它似乎只能在我的localhost上运行。如果我在抛出异常后尝试访问/elmah.axd,服务器只是告诉我该资源不存在。跆拳道?我以为Elmah会照顾路线。

有趣的是,如果我不使用迁移和种子方法,ASP 会创建数据库表。换句话说:如果我只是发布标准模板MVC应用程序并尝试注册,它会在数据库中创建表并插入新注册的用户数据。这告诉我们连接字符串没有错,对吧? 那么IdentityDbContext如何可以创建表并插入数据,但是当我尝试使用ApplicationContext(派生自IdentityDbContext)通过Configuration.cs种子时,它不起作用。没有创建表,也没有插入数据。为什么?!我一直在撕裂我的头发24小时试图解决这个问题。请帮帮我。

无论如何,当我进入帐户/登录并输入我的凭据时,它会说:

  

为上下文”ApplicationDbContext“启用了迁移,但是   数据库不存在或不包含映射表。使用迁移   停止数据库及其表,例如通过运行   来自程序包管理器控制台的“Update-Database”命令。

我知道它不存在!这就是我需要你运行Initial_Create Migration!

的原因

我也开玩笑并设法获得内部服务器错误(500),但我无法调试会发生什么。

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>

1 个答案:

答案 0 :(得分:2)

这是一个古老的话题,但我会尝试解释一些事情。

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

这是一个解释如何: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或EF 6,我发现如果你将域设置为.Net Framework 4.5.1(或4.5我不记得哪一个是有效的替代品)你确实可以发布一个MVC 5与EF6。

我自己完成了几次,仅使用Asp.Net Identity Version 1.0.0,所以我不能保证支持更新的版本。