C# 代码优先迁移过程:我缺少什么部分?

C# 代码优先迁移过程:我缺少什么部分?,c#,asp.net,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net,Asp.net Mvc,Entity Framework,Ef Code First,我的步骤: 1使用查询在SSMS中创建我的数据库 /* Execute in SQL Server Management Studio prior to building data model(s) */ CREATE DATABASE snakedb; GO USE snakedb; /* Create table of scores of games played. Every game will have a score recorded, but there wil

我的步骤:

1使用查询在SSMS中创建我的数据库

/* Execute in SQL Server Management Studio prior to building data model(s) */

CREATE DATABASE snakedb; 

GO

USE snakedb;

/*
   Create table of scores of games played. Every game will have a score recorded, but there
   will only be a corresponding name if the user enters one
*/
CREATE TABLE Scores ( id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
                      score int NOT NULL,
                      name VARCHAR (50) 
                    );

/* 
    Create table of text logs of the games played. These are reviewed to sniff out cheating.  
*/
CREATE TABLE GameLogs ( id int IDENTITY(1,1) NOT NULL PRIMARY KEY, 
                        scoreId INT NOT NULL FOREIGN KEY REFERENCES scores(id) ON DELETE CASCADE ON UPDATE CASCADE, 
                        logText VARCHAR (8000)
                       ); 

/*
    Table of unique IP addresses that have visited the site. The 4 decimal numbers separated by dots that compose each
    IP address, e.g. the 172, 16, 254 and 1 in 172.16.254.1, correspond to the 4 columns byte1, byte2, byte3 and byte4
*/
CREATE TABLE IPs ( id int IDENTITY (1,1) NOT NULL PRIMARY KEY, 
                   byte1 tinyint, 
                   byte2 tinyint, 
                   byte3 tinyint, 
                   byte4 tinyint 
                  );

/*
     Table of banned IP addresses 
*/
CREATE TABLE BannedIPs ( id int IDENTITY (1,1) NOT NULL PRIMARY KEY, 
                         ipId int NOT NULL FOREIGN KEY REFERENCES IPs(id)
                        );
2右键单击Migrations文件夹->Add New Item->ADO.NET实体数据模型->Code First from database->通过向导选择新创建的snakedb并创建相应的C文件

3现在我的迁移文件夹中有了新文件BannedIP.cs、Configuration.cs、GameLog.cs、IP.cs、Score.cs和SnakeDB.cs

4要按照此处的说明对数据库进行种子设定,我将Configuration.cs更改为

在PackageManager控制台中,获取输出

Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201509300534414_Initial].
Applying explicit migration: 201509300534414_Initial.
Running Seed method.
但当我回到SSMS时,没有创建任何数据库

有什么我遗漏的吗

还有,说明书上说

第一个命令生成创建数据库的代码 第二个命令执行该代码。数据库是在本地创建的, 使用LocalDB


我想知道我是否也可以用远程数据库来实现这一点。是否有办法创建ASP.NET项目,以便发布而不是在控制台中运行命令,为数据库种子?

您的数据库上下文定义应如下所示:

public class ApplicationDbContext: DbContext
{
    public ApplicationDbContext() : base("connectionString")
    {
    }

    public DbSet<Scores> Scores { get; set; }
    public DbSet<GameLogs> GameLogs { get; set; }
    public DbSet<IPs> IPs { get; set; }
    public DbSet<BannedIPs> BannedIPs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}
internal sealed class Configuration : DbMigrationsConfiguration<MigratingDatabase.SchoolContext>
{
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(MigratingDatabase.SchoolContext context)
        {

        }
}
必须在web.config或app.config中定义:

我可以在它从数据库上下文继承的配置类中看到:

DbMigrationsConfiguration<SnakeGame.Models.ApplicationDbContext>
我想应该是:

protected void Seed (SnakeGame.Models.ApplicationDbContext context)
{

}
一切就绪后,您可以运行:

Update-Database -Verbose
它应该为你建立数据库

要启用迁移,您必须做的另一件事是更改配置类的构造函数:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

使用AutomaticMigrationsEnabled=true。

因为更新数据库时没有错误,所以我假设它更新了您的LocalDb。您是否为SQL数据库创建了一个连接字符串,并将上下文指向它显示上下文代码?您是否使用将创建数据库的数据库初始值设定项?另外,也不知道为什么要用代码创建数据库,然后反过来对其进行反向工程。代码的力量首先是你可以设计你的实体类POCO,让EF为你创建表和关系。嘿,科纳尔德,有没有关于我答案的反馈?干杯
DbMigrationsConfiguration<SnakeGame.Models.ApplicationDbContext>
protected void Seed (SnakeGame.Migrations.SnakeDB context)
{

}
protected void Seed (SnakeGame.Models.ApplicationDbContext context)
{

}
Update-Database -Verbose
public Configuration()
{
    AutomaticMigrationsEnabled = true;
}