SimpleMembership表未在远程服务器上创建(本地工作正常)EF MVC4

时间:2014-07-23 21:29:26

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

我正在尝试扩展简单成员功能,我在使用update-database创建表并从种子方法填充表时遇到问题。 本地命令

 update-database

创建所有表并正确填充它们,包括webpages_Membership等。 当我尝试使用此命令在服务器上更新它时:

  update-database -ConnectionStringName "remote" -Verbose

只有我自己设计的表格才会被创建和填充。

下载我从服务器获得的内容:

  PM> update-database -ConnectionStringName "remote" -Verbose
Using StartUp project 'Phones'.
Using NuGet project 'Phones'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'databasename' (DataSource: tcp:server,port, Provider: System.Data.SqlClient, Origin: Explicit).
No pending code-based migrations.
Applying automatic migration: 201407232047491_AutomaticMigration.
CREATE TABLE [dbo].[UserProfile] (
    [UserId] [int] NOT NULL IDENTITY,
    [UserName] [nvarchar](max),
    [FirstName] [nvarchar](max),
    [LastName] [nvarchar](max),
    [Email] [nvarchar](max),
    [Balance] [float] NOT NULL,
    [Enabled] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.UserProfile] PRIMARY KEY ([UserId])
)
CREATE TABLE [dbo].[Orders] (
    [OrderId] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    [IMEI] [nvarchar](max),
    [UnlockCode] [nvarchar](max),
    [UserProfile_UserId] [int],
    [Phone_PhoneId] [int],
    CONSTRAINT [PK_dbo.Orders] PRIMARY KEY ([OrderId])
)
CREATE TABLE [dbo].[Phones] (
    [PhoneId] [int] NOT NULL IDENTITY,
    [Manufacturer] [nvarchar](max),
    [Model] [nvarchar](max),
    [UnlockPrice] [float] NOT NULL,
    CONSTRAINT [PK_dbo.Phones] PRIMARY KEY ([PhoneId])
)
CREATE INDEX [IX_UserProfile_UserId] ON [dbo].[Orders]([UserProfile_UserId])
CREATE INDEX [IX_Phone_PhoneId] ON [dbo].[Orders]([Phone_PhoneId])
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [FK_dbo.Orders_dbo.UserProfile_UserProfile_UserId] FOREIGN KEY ([UserProfile_UserId]) REFERENCES [dbo].[UserProfile] ([UserId])
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [FK_dbo.Orders_dbo.Phones_Phone_PhoneId] FOREIGN KEY ([Phone_PhoneId]) REFERENCES [dbo].[Phones] ([PhoneId])
CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](255) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId])
)
BEGIN TRY
    EXEC sp_MS_marksystemobject 'dbo.__MigrationHistory'
END TRY
BEGIN CATCH
END CATCH
[Inserting migration history record]
Running Seed method.

来自本地:

PM> update-database -Verbose
Using StartUp project 'Phones'.
Using NuGet project 'Phones'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'phones' (DataSource: localhost\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Configuration).
No pending code-based migrations.
Applying automatic migration: 201407232046599_AutomaticMigration.
CREATE TABLE [dbo].[UserProfile] (
    [UserId] [int] NOT NULL IDENTITY,
    [UserName] [nvarchar](max),
    [FirstName] [nvarchar](max),
    [LastName] [nvarchar](max),
    [Email] [nvarchar](max),
    [Balance] [float] NOT NULL,
    [Enabled] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.UserProfile] PRIMARY KEY ([UserId])
)
CREATE TABLE [dbo].[Orders] (
    [OrderId] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    [IMEI] [nvarchar](max),
    [UnlockCode] [nvarchar](max),
    [UserProfile_UserId] [int],
    [Phone_PhoneId] [int],
    CONSTRAINT [PK_dbo.Orders] PRIMARY KEY ([OrderId])
)
CREATE TABLE [dbo].[Phones] (
    [PhoneId] [int] NOT NULL IDENTITY,
    [Manufacturer] [nvarchar](max),
    [Model] [nvarchar](max),
    [UnlockPrice] [float] NOT NULL,
    CONSTRAINT [PK_dbo.Phones] PRIMARY KEY ([PhoneId])
)
CREATE INDEX [IX_UserProfile_UserId] ON [dbo].[Orders]([UserProfile_UserId])
CREATE INDEX [IX_Phone_PhoneId] ON [dbo].[Orders]([Phone_PhoneId])
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [FK_dbo.Orders_dbo.UserProfile_UserProfile_UserId] FOREIGN KEY ([UserProfile_UserId]) REFERENCES [dbo].[UserProfile] ([UserId])
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [FK_dbo.Orders_dbo.Phones_Phone_PhoneId] FOREIGN KEY ([Phone_PhoneId]) REFERENCES [dbo].[Phones] ([PhoneId])
CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](255) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId])
)
BEGIN TRY
    EXEC sp_MS_marksystemobject 'dbo.__MigrationHistory'
END TRY
BEGIN CATCH
END CATCH
[Inserting migration history record]
Running Seed method.

我的班级种子方法:

internal sealed class Configuration : DbMigrationsConfiguration<Phones.Models.UsersContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;

        }

        protected override void Seed(Phones.Models.UsersContext context)
        {
            if (!WebMatrix.WebData.WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection(
                   "DefaultConnection",
                   "UserProfile",
                   "UserId",
                   "UserName", autoCreateTables: true);
            }
            if (!Roles.RoleExists("Administrator"))
                Roles.CreateRole("Administrator");
            if (!Roles.RoleExists("User"))
                Roles.CreateRole("User");

            if (!WebSecurity.UserExists("Admin"))
                WebSecurity.CreateUserAndAccount(
                    "Admin",
                    "pass",
                    new { FirstName = "testname", LastName = "testsurname", Email = "email@email.com", Balance = 0, Enabled = true });

            if (!Roles.GetRolesForUser("Admin").Contains("Administrator"))
                Roles.AddUsersToRoles(new[] { "Admin" }, new[] { "Administrator" });
        }
    }

这是我的UsersContext类:

public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Order> Orders { get; set; }

        public DbSet<Phone> Phones { get; set; }

    }

非常感谢任何帮助。 最好的祝福, 马丁

2 个答案:

答案 0 :(得分:0)

您是否尝试在远程服务器上使用visual studio命令提示符?

如果打开visual studio命令提示符(在开始时,键入visual studio命令提示符,它应该出现)。

然后在命令提示符下键入:

Aspnet_regsql.exe

这将打开一个旨在为asp.net网站创建成员资格架构的程序。您也可以从此应用程序远程连接到数据库。

当应用程序打开时:

  • 单击“为应用程序服务配置SQL Server”
  • 点击下一步,系统会提示您是否要远程连接。

Remote connection example

  • 输入您的凭据然后点击下一步。您将获得要连接的数据库及其所在服务器的设置摘要。检查一切都好,然后点击下一步。
  • 按照说明操作,它将安装用于会员资格的必要ASP_数据库(ROLES等)。

答案 1 :(得分:0)

以这种方式使用update-database命令实际上是在开发期间的一个捷径,并不打算用于设置用于测试或生产的远程服务器。对于远程服务器,请使用带有update-database命令的script参数创建sql脚本,以创建或更新现有数据库,然后在该服务器上运行该脚本。