代码优先迁移过程:我错过了哪个部分?
我的步骤:
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, eg 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)现在我在我的Migrations文件夹中有新文件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 { 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) I DROP
数据库snakedb
因为据我所知,我将能够重新创建它并在下一步中添加一些测试数据
6)我跑
Add-Migration Initial Update-Database
在Package Manager控制台中获取输出
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 { get; set; } public DbSet GameLogs { get; set; } public DbSet IPs { get; set; } public DbSet BannedIPs { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // modelBuilder.Conventions.Remove(); } }
它inheritance自DbContext
并定义对象模型的结构。
你的DbSet(s)
应该映射到你的类:
- 比分
- GameLogs
- IP地址
- BannedIPs
如您所见,构造函数需要一个连接字符串:
public ApplicationDbContext() : base("connectionString") { }
必须在web.config
(或app.config
)中定义:
在
部分。 我在这里使用了localhost
,当然,你可以使用你的远程数据库。
现在,从Package Manager Console
您必须使用Enable-Migrations
。
此命令将构建配置文件,该文件应如下所示:
internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(MigratingDatabase.SchoolContext context) { } }
我在Configuration
类中可以看到它从数据库上下文inheritance:
DbMigrationsConfiguration
但它试图播种另一个对象:
protected void Seed (SnakeGame.Migrations.SnakeDB context) { }
而且,我想,它应该是:
protected void Seed (SnakeGame.Models.ApplicationDbContext context) { }
当一切都到位时,您可以运行:
Update-Database -Verbose
它应该为你建立数据库。
要启用迁移,您需要做的另一件事是更改配置类的构造函数:
public Configuration() { AutomaticMigrationsEnabled = true; }
使用AutomaticMigrationsEnabled = true
。