我的步驟:代碼優先遷移過程:我缺少什麼部分?
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.cs
,Configuration.cs
,GameLog.cs
,IP.cs
,Score.cs
和SnakeDB.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項目成爲發佈,而不是在控制檯中運行命令,爲數據庫播種?
由於update-database沒有錯誤,我假設它更新了你的LocalDb。你是否爲你的SQL數據庫創建了一個連接字符串,並將它指向它(顯示上下文代碼)?你使用的數據庫初始化器會創建數據庫嗎?此外,不知道爲什麼你要在代碼中創建數據庫,然後轉向並逆向工程。代碼的優勢在於您可以設計您的實體類(POCO)並讓EF爲您創建表格和關係。 –
Hey Konald,對我的回答有任何反饋?乾杯。 – LeftyX