以åãEntityFrameworkã§ã®èªåãã¤ã°ã¬ã¼ã·ã§ã³ã試ãã¦ã¿ã¾ãããããªãã ããã¾ãããã¾ããã§ããã
ãããªä¸ãã.NETéçºãã¯ããã¸å ¥é 2014å¹´ç VisualStudio2013対å¿ç (MSDNããã°ã©ãã³ã°ã·ãªã¼ãº)ããèªãã§ã¡ãã£ã¨åå¼·ããã®ã§æãåããã¦ã¿ããã¨ã«ãã¾ãã æ¥æ¬èªãä¸èªç±ãããªã®ã¯å¤§ç®ã«è¦ã¦ä¸ããã
åæãµã³ãã«ã½ã¼ã¹
ã¾ãã¯ãããªã½ã¼ã¹ãç¨æãã¾ããããã®ç¶æ ã§ã¯ã¾ã Enable-Migrationãå®è¡ãã¦ãã¾ããã
class Program { static void Main(string[] args) { AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory); Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AppDbContext>()); var db = new AppDbContext(); db.Articles.Add(new Article() { Title = "test" }); db.SaveChanges(); } } public class AppDbContext : DbContext { public DbSet<Article> Articles { get; set; } } public class Article { public int Id { get; set; } public string Title { get; set; } }
ãã¼ã¿ãã¼ã¹ã®åæåã¯ä¸æ¦ãDropCreateDatabaseIfModelChangesãæå®ãã¾ãããä¸åº¦å®è¡ãã¦ãã¼ã¿ãã¼ã¹ãä½æãããã®ã確èªããã
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AppDbContext>());
ã®è¡èªä½ãåé¤ãã¦ããã¾ãã
ã¢ãã«ãå¤æ´
ã¢ãã«ã«ããããã£ï¼Contentï¼ã追å ãã¾ãã
public class Article { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } }
ããã§ããã°ã©ã ãå®è¡ããã¨ã¢ãã«ãå¤æ´ã«ãªã£ã¦ããã®ã§ä»¥ä¸ã®æ§ãªä¾å¤ãçºçãã¾ãã
The model backing the 'AppDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
ããã§ããã¤ã°ã¬ã¼ã·ã§ã³ãæå¹ã«ãã¾ãããã
èªåãã¤ã°ã¬ã¼ã·ã§ã³ã®è¨å®
ããã±ã¼ã¸ããã¼ã¸ã£ã³ã³ã½ã¼ã«ãã以ä¸ã®ã³ãã³ããå®è¡ãã¦ãèªåãã¤ã°ã¬ã¼ã·ã§ã³ãæå¹ã«ãã¾ãã
PM> Enable-Migrations -EnableAutomaticMigrations
-EnableAutomaticMigrationsãä»ããã¨ãã¨ä»ããªãã£ãã¨ãã®éãã¯ãConfigurationã¯ã©ã¹ã®AutomaticMigrationsEnabled ããããã£ãtrueã«ãªãããfalseã«ãªããã®éãã®ããã§ãã
public Configuration() { AutomaticMigrationsEnabled = true; ContextKey = "EFSample.AppDbContext"; }
ãããåãæ¿ããã°èªå<=>æåãã¤ã°ã¬ã¼ã·ã§ã³ã®åãæ¿ããã§ãããã§ãã
ããã§å度ããã°ã©ã ãå®è¡ãã¦ã¿ãã¨ãã¯ãå ã»ã©ã¨åãä¾å¤ãçºçãã¾ãã
ãã¼ã¿ãã¼ã¹ã®èªåãã¤ã°ã¬ã¼ã·ã§ã³ãæåã§è¡ã
ããã±ã¼ã¸ããã¼ã¸ã£ã³ã³ã½ã¼ã«ãã以ä¸ã®ã³ãã³ããå®è¡ãã¦ããã¼ã¿ãã¼ã¹ããã¤ã°ã¬ã¼ã·ã§ã³ãã¾ãã
PM> Update-Database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. No pending explicit migrations. Applying automatic migration: 201407041710393_AutomaticMigration. Running Seed method.
ããããã¢ãã«ãå¤æ´ãã度ã«ãUpdate-Databaseããå®è¡ããã®ã¯æéããããã¨æãã¾ããããã§ã次ã®æ§ã«ãã¦èªåã§ãã¤ã°ã¬ã¼ã·ã§ã³ãããæ§ã«ãã¾ãã
ãã¼ã¿ãã¼ã¹ã®èªåãã¤ã°ã¬ã¼ã·ã§ã³ãèªåã§è¡ã
以ä¸ã®ããã«MigrateDatabaseToLatestVersionã§åæåããã¨ã¢ããªã±ã¼ã·ã§ã³å®è¡æã«èªåã§ãã¼ã¿ãã¼ã¹ããã¤ã°ã¬ã¼ã·ã§ã³ãã¦ãããããã«ãªãã¾ãã
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, Configuration>());
èªåãã¤ã°ã¬ã¼ã·ã§ã³ã§ãã¼ã¿ãæ¶ããã¨ããã
èªåãã¤ã°ã¬ã¼ã·ã§ã³ã¯æéããããã楽ã¡ãã§ããããã¼ãã«ãDropå¾ã«Createããªãã¨ãããªããããªã¢ãã«ã®å¤æ´ããã£ãå ´åã«ã¯ã©ããªãã®ã§ããããã
試ãã«Articleã¯ã©ã¹ã®Contentããããã£ãåé¤ãã¦å®è¡ãã¦ã¿ã¾ããããããã¨ä»¥ä¸ã®æ§ãªä¾å¤ãçºçãã¾ãã
Automatic migration was not applied because it would result in data loss. Set AutomaticMigrationDataLossAllowed to 'true' on your DbMigrationsConfiguration to allow application of automatic migrations even if they might cause data loss. Alternately, use Update-Database with the '-Force' option, or scaffold an explicit migration.
ããã¼ã¿åªå¤±ãçºçããããèªåãã¤ã°ã¬ã¼ã·ã§ã³ã¯ããªãã£ãããã¨ããäºã«ãªãã¾ããå®å¿ã§ããã
å¿ è¦ã«å¿ãã¦ãã¼ã¿ã®ããã¯ã¢ãããåãããUpdate-Database -Forceãããã°è¯ãã¨æãã¾ãã
使ãæ
ã.NETéçºãã¯ããã¸å ¥é 2014å¹´ç VisualStudio2013対å¿ç (MSDNããã°ã©ãã³ã°ã·ãªã¼ãº)ãã«ãæ¸ãã¦ããã¾ããããèªåãã¤ã°ã¬ã¼ã·ã§ã³ã¯ã¢ãã«ãé »ç¹ã«å¤æ´ããéçºåæã®æ®µéã§ä½¿ç¨ããã¨ããã¨æãã¾ããããç¨åº¦ã¢ãã«ãåºã¾ã£ã¦ãã段éï¼ãããã¯ãªãªã¼ã¹å¾ï¼ã§æåãã¤ã°ã¬ã¼ã·ã§ã³ã«åãæ¿ããã®ããããããã¨æãã¾ãã
- ä½è : Brian Driscoll,Nitin Gupta,Robert Vettor,Zeeshan Hirani,Larry Tenny
- åºç社/ã¡ã¼ã«ã¼: Apress
- çºå£²æ¥: 2013/10/28
- ã¡ãã£ã¢: Kindleç
- ãã®ååãå«ãããã°ãè¦ã