c# - Code-First Migration Process: What part am I missing? -
my steps:
1) create database in ssms query
/* execute in sql server management studio prior building data model(s) */ create database snakedb; go use snakedb; /* create table of scores of games played. every game have score recorded, there corresponding name if user enters 1 */ 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 games played. these reviewed 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 have visited site. 4 decimal numbers separated dots compose each ip address, e.g. 172, 16, 254 , 1 in 172.16.254.1, correspond 4 columns byte1, byte2, byte3 , 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) right-click migrations folder -> add new item -> ado.net entity data model -> code first database -> (go through wizard select newly created snakedb
, create corresponding c# files)
3) have in migrations folder new files bannedip.cs
, configuration.cs
, gamelog.cs
, ip.cs
, score.cs
, snakedb.cs
4) procedure seed database according instructions here, change 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 seeding database context.ips.addorupdate( => 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.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
database snakedb
because, understand, able recreate , add test data in next step
6) run
add-migration initial update-database
in package manager console , output
specify '-verbose' flag view sql statements being applied target database. applying explicit migrations: [201509300534414_initial]. applying explicit migration: 201509300534414_initial. running seed method.
but when go ssms, no database has been created.
is there i'm missing?
also, instructions say
the first command generates code creates database, , second command executes code. database created locally, using localdb.
and i'm wondering whether possible me remote db well. there way make asp.net project publishing, rather running commands in console, seeds database?
your database context definition should this:
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>(); } }
it inherits dbcontext
, defines structure of object model.
your dbset(s)
should mapped classes:
- scores
- gamelogs
- ips
- bannedips
as can see constructor needs connection string:
public applicationdbcontext() : base("connectionstring") { }
which must defined in web.config
(or app.config
):
<connectionstrings> <add name="connectionstring" connectionstring="server=localhost;database=migrationstest;trusted_connection=true;" providername="system.data.sqlclient" /> </connectionstrings>
inside <configuration>
section. i've used localhost
here but, of course, can use remote database.
now package manager console
must have enabled migration enable-migrations
.
command build configuration file should this:
internal sealed class configuration : dbmigrationsconfiguration<migratingdatabase.schoolcontext> { public configuration() { automaticmigrationsenabled = true; } protected override void seed(migratingdatabase.schoolcontext context) { } }
what can see in configuration
class inherits database context:
dbmigrationsconfiguration<snakegame.models.applicationdbcontext>
but trying seed different object:
protected void seed (snakegame.migrations.snakedb context) { }
and, guess, should be:
protected void seed (snakegame.models.applicationdbcontext context) { }
when in place can run:
update-database -verbose
and should build database you.
another thing have enable migration change constructor of configuration class:
public configuration() { automaticmigrationsenabled = true; }
using automaticmigrationsenabled = true
.
Comments
Post a Comment