2015-09-30 82 views
0

我的步驟:代碼優先遷移過程:我缺少什麼部分?

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)右鍵單擊文件夾遷移 - >添加新項 - > ADO.NET實體數據模型 - >從數據庫代碼第一次 - >(瀏覽嚮導,選擇新創建的snakedb,並創建相應的C#文件)

3)現在,我有我的遷移百分比ations文件夾新文件BannedIP.csConfiguration.csGameLog.csIP.csScore.csSnakeDB.cs

4)一個過程種子根據這裏的說明我的數據庫,我改變我的Configuration.cs

namespace SnakeGame.Migrations 
{ 
    using System; 
    using System.Data.Entity; 
    using System.Data.Entity.Migrations; 
    using System.Linq; 
    using SnakeGame.Models; 

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

     //protected override void Seed (SnakeGame.Migrations.SnakeDB context) 
     protected void Seed (SnakeGame.Migrations.SnakeDB context) 
     { 

      // Test data for seeding the database 

      context.IPs.AddOrUpdate(
       i => i.id, 
       new IP() { id = 1, byte1 = 4, byte2 = 35, byte3 = 241, byte4 = 179 }, 
       new IP() { id = 2, byte1 = 172, byte2 = 16, byte3 = 254, byte4 = 1 } 
      ); 

      context.BannedIPs.AddOrUpdate(
       i => i.id, 
       new BannedIP() { id = 1, ipId = 1} 
      ); 

      context.Scores.AddOrUpdate(
       s => s.id, 
       new Score() { id = 1, score1 = 12, name = "John Skeet" }, 
       new Score() { id = 2, score1 = 1923, name = "Steve Ballmer"} 
      ); 
     } 
    } 
} 

5 )DROP數據庫snakedb因爲,據我所知,我將能夠重新創建它,並在下一步添加一些測試數據

6)我在Package Manager控制檯運行

Add-Migration Initial 
Update-Database 

,並得到輸出

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項目成爲發佈,而不是在控制檯中運行命令,爲數據庫播種?

+1

由於update-database沒有錯誤,我假設它更新了你的LocalDb。你是否爲你的SQL數據庫創建了一個連接字符串,並將它指向它(顯示上下文代碼)?你使用的數據庫初始化器會創建數據庫嗎?此外,不知道爲什麼你要在代碼中創建數據庫,然後轉向並逆向工程。代碼的優勢在於您可以設計您的實體類(POCO)並讓EF爲您創建表格和關係。 –

+0

Hey Konald,對我的回答有任何反饋?乾杯。 – LeftyX

回答

0

你的數據庫上下文的定義應該是這個樣子:

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>(); 
    } 
} 

DbContext繼承並定義你的對象模型的結構。

DbSet(s)應該映射到你的類:

  • 成績
  • GameLogs
  • IP地址
  • BannedIPs

正如你所看到的構造函數需要一個連接字符串:

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

其必須在web.config(或app.config)來定義:

<connectionStrings> 
    <add name="connectionString" connectionString="Server=localhost;Database=MigrationsTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

內部<configuration>部。我在這裏使用了localhost,但是,當然,您可以使用遠程數據庫。

現在來自Package Manager Console您必須啓用了與Enable-Migrations的遷移。
這個命令將構建應該是這個樣子的配置文件:

internal sealed class Configuration : DbMigrationsConfiguration<MigratingDatabase.SchoolContext> 
{ 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = true; 
     } 

     protected override void Seed(MigratingDatabase.SchoolContext context) 
     { 

     } 
} 

我可以在你的Configuration類看到它從數據庫上下文繼承:

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; 
} 

使用AutomaticMigrationsEnabled = true