Entity Frameworkã®Code Firstã¢ãããŒãã§ã¯ãç¬èªã®ãã¡ã€ã³ã¯ã©ã¹ã䜿çšããŠãEFãã¯ãšãªã®äœæãå€æŽã®è¿œè·¡ãããã³æŽæ°ã«äœ¿çšããã¢ãã«ãè¡šãããšãã§ããŸãã Code Firstã¯ãæ§æèŠåãšåŒã°ããããã°ã©ãã³ã°ãã¿ãŒã³ã䜿çšããŸãã ããã¯ãã¯ã©ã¹ãEFãæŠå¿µã¢ãã«ã«äœ¿çšããã¹ããŒãã®èŠåã«åŸãããšãCode Firstãä¿¡ããŠããããšãæå³ããŸãã ãã®å ŽåãEFã¯å¿ èŠãªéšåã䜿çšããŠãã®æ©èœãå®è¡ã§ããŸãã ãã ããã¯ã©ã¹ãèŠåãæ£ãã䜿çšããªãå Žåã¯ãEFãããããæ£ããç解ã§ããããã«ãå¿ èŠãªæ§æãæåã§è¿œå ã§ããŸãã
Code Firsã¢ãããŒãã䜿çšãããšããããã®æ§æã2ã€ã®æ¹æ³ã§å®çŸ©ã§ããŸãã 1ã€ã¯ãDataAnnotationsãšåŒã°ããåçŽãªå±æ§ã䜿çšããããšã§ãã 2çªç®ã®æ¹æ³ã¯ãFluent APIã䜿çšããããšã§ããããã«ãããæ§æãã³ãŒãã§åœä»€çã«èšè¿°ã§ããŸãã
ãã®èšäºã§ã¯ãFluent APIã䜿çšããã«ã¹ã¿ãã€ãºã«çŠç¹ãåœãŠãŠããŸãã Code Firsã®èŠåã¯ãåå«ãŸãã¯åã ã®ã¯ã©ã¹ãæãããããã£ã«åºã¥ããŠã¯ã©ã¹éã®é¢ä¿ãèšè¿°ããã®ã«éåžžã«åœ¹ç«ã¡ãŸãã ã¯ã©ã¹ã«å€éšããŒããªãå ŽåãCode Firsã¯ããããèªåã§äœæã§ããŸãã ãã ããã¯ã©ã¹ã®èª¬æã§ã¯é¢ä¿ã«é¢ããååãªæ å ±ãæäŸãããªãå ŽåããããããCode Firsã¯ãã¹ãŠãæ£ããç解ãããæ¬ èœãéšåãæ£ããè¿œå ã§ããŸãã
ã¢ãã«ãèãã
ãŸããBlogãšPostã®2ã€ã®åçŽãªã¯ã©ã¹ããå§ããŸããããBlogã¯Postãš1察å€ã®é¢ä¿ã«ãããŸãã
public class Blog { public int Id { get; set; } public string Title { get; set; } public string BloggerName { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int Id { get; set; } public string Title { get; set; } public DateTime DateCreated { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
1察å€ã®é¢ä¿ã®èŠåãç解ãã
ã¯ã©ã¹ã§1察å€ã®é¢ä¿ãå®çŸ©ããäžè¬çãªæ¹æ³ã®1ã€ã¯ã1ã€ã®ã¯ã©ã¹ã®åã³ã¬ã¯ã·ã§ã³ãšå€éšããŒããåã¯ã©ã¹ã®ããã²ãŒã·ã§ã³ããããã£ãšå ±ã«äœæããããšã§ãã äžèšã®äŸã§ã¯ãBlogã«ã¯Postsããããã£ããããŸããããã¯ãPoståã®ICollectionã®ã³ã¬ã¯ã·ã§ã³ã§ãã ãããŠãPostã«ã¯å€éšããŒBlogIDããããŸããããã¯Blogããã²ãŒã·ã§ã³ããããã£ãšãåŒã°ããBlogã®ç¥å ãæããŸãã
ãã®ããŒã¿ã¯ãCode FirstãèŠåã«åºã¥ããŠããŒã¿ããŒã¹ã«æ¬¡ã®ããŒãã«ãäœæããã®ã«ååã§ãã
Code Firstã«ãã£ãŠBlogIdãã£ãŒã«ããå€éšããŒã«ãªãïŒPosts.BlogIdã®äž»ããŒ/å€éšããŒãšBlogs.Idã®å¶éã決å®ãããŸãïŒãnulläžå¯ã§ããããšã«æ³šæããŠãã ããã Code Firstã¯ãã¯ã©ã¹ã§èª¬æãããŠããèŠåã«åºã¥ããŠãããã®çµè«ãåŒãåºããŸããã
å€éšããŒããããã£ãæå®ãããŠããªãå Žåã«HasRequiredã䜿çšãã
Postã¯ã©ã¹ã§BlogIdããããã£ãèšå®ãããBlogããã²ãŒã·ã§ã³ããããã£ãèšå®ãããšã©ããªããŸããã Code Firstã¯ãBlogããããã£ãBlogãšã³ãã£ãã£ãæãããšãç¥ã£ãŠãããããå¿ èŠãªé¢ä¿ãäœæããŸãã 圌ã¯ãå³2ã«ç€ºãããã«ãPosts.Blog_Idå€éšããŒãäœæããBlog.Idã«é¢é£ä»ããŸãã
ãã ããéèŠãªè©³çŽ°ã1ã€ãããŸãã Blog_Idã¯NULLå¯èœã§ãã ãã®åŸãããã°ã«é¢é£ããªãæçš¿ãè¿œå ããããšãå¯èœã«ãªããŸãã ã ããã³ãŒããã¡ãŒã¹ãã¯ãã¯ã©ã¹ã®æ £ç¿ãç 究ããŠããããç解ããŸããããããã¯éçºè ãæ±ããŠãããã®ã§ã¯ãããŸããã Fluent APIã䜿çšããŠããã®äžæ£ç¢ºããä¿®æ£ããŸãã
Fluent APIæ§æã¯ãã¯ã©ã¹ããã¢ãã«ãæ§ç¯ãããšãã«Code Firstã§äœ¿çšãããŸãã 以äžã«ç€ºãããã«ãDbContextã¯ã©ã¹ã®OnModelCreatingã¡ãœããããªãŒããŒã©ã€ãããããšã«ãããæ§æãåã蟌ãããšãã§ããŸãã
public class BlogContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //configure model with fluent API }
DbModelBuilderã䜿çšãããšãèšå®ãååããããšãã§ããŸãã ãšã³ãã£ãã£ã®1ã€ãå€æŽããããšãã¢ãã«ãã«ããŒã«éç¥ããŸãã ãã®ããã«ããžã§ããªãã¯ã䜿çšããPostã®æ¬è³ªãå€æŽããŸãã ã¢ã¯ã»ã¹ãããšãé¢ä¿ã®äžéšã§ããHasRequiredã¡ãœããã䜿çšããŠãããã²ãŒã·ã§ã³ããããã£ïŒãã®å Žåã¯Blogããããã£ïŒãå¿ èŠã§ããããšã瀺ãããšãå¯èœã«ãªããŸãã
modelBuilder.Entity<Post>().HasRequired(p => p.Blog);
çµæã¯äºéã®å¹æã§ãã 1ã€ç®ã¯ãBlog_Idãã£ãŒã«ããåã³nullã«ãªããªãããšã§ãã ãŸããEFã¯ãªã³ããã³ãã§ããŸãã¯ããŒã¿ããŒã¹ã«ä¿åããåã«æ€èšŒãå®è¡ãããããæå®ããããã¹ãŠã®æ¡ä»¶ãæºããããŠããããšã確èªã§ããŸãã
ã«ã¹ã¿ã å€éšããŒåã®ã«ã¹ã¿ãã€ãº
å€éšããŒãæå®ããå Žåã§ãããã®ååã¯Code Firstã®èŠåãšåžžã«äžèŽãããšã¯éããŸããã æ £äŸã«ãããããŒåã¯ã¯ã©ã¹ã®ååãŸãã¯ãclassname_IdããŸãã¯ãclassnameIdããšäžèŽããå¿ èŠããããŸãã ãã®ãããCode Firstã¯å ã®ã¯ã©ã¹ããããã£ã§ããBlogIdã§é©åã«åäœã§ããŸããã
ããããååãæ £äŸã«åŸã£ãŠããªãå Žåã¯ã©ããªããŸããïŒ ããšãã°ããFKã+ parent_class_name +ãIdãã§ãã
public int FKBlogId { get; set; }
Code Firstã¯ãFKBlogIdãå€éšããŒãšããŠæ©èœããå¿ èŠãããããšãç¥ããŸããã Blogããããã£ããªã³ã¯ããããã«å¿ èŠã§ãããããFKBlogIdããã³Blog_Idã®æšæºåãäœæããŸããããã¯å€éšããŒã«ãªããŸãã
ãŸããBlogãšPostãããã«äœ¿çšããŠãFKBlogIdã¯ãããã°ãæãå€éšããŒãšããŠããã°ã©ã ã«ãã£ãŠèªèãããããšã¯ãããŸããã
Fluent APIã䜿çšãããšããã®åé¡ã解決ã§ããŸããFKBlogIdãããã°ã«é¢é£ããå€éšããŒãšããŠäœ¿çšããŸãã
èšå®æã«ã¯ããªã¬ãŒã·ã§ã³ã·ããã®äž¡ç«¯ãæå®ããå¿ èŠããããŸããè€æ°ã®ãªã¬ãŒã·ã§ã³ã·ããã瀺ãããã°ã®ããããã£ïŒæçš¿ïŒãšã芪ãæããã€ã³ãã£ã³ã°ããããã£ïŒããã°ïŒã§ãã
ãŸããWithManyã¡ãœãããè¿œå ããå¿ èŠããããŸããããã«ãããè€æ°ã®é¢ä¿ãå«ãŸããBlogããããã£ãæå®ã§ããŸãã
modelBuilder.Entity<Post>().HasRequired(p => p.Blog) .WithMany(b => b.Posts)
次ã«ãHasForeignKeyã¡ãœãããè¿œå ããŸããããã¯ãã©ã®Postããããã£ãå€éšããŒã§ãããããã°ãæããã瀺ããŸãã å®å šãªã³ãŒãïŒ
modelBuilder.Entity<Post>().HasRequired(p => p.Blog) .WithMany(b => b.Posts) .HasForeignKey(p => p.FKBlogId);
éçºè ãææ¡ããããŒã¿ããŒã¹ã¹ããŒããšé¢ä¿ã®æ£ããæ§ç¯ã«å¿ èŠãªãã¹ãŠã®ããŒã¿ãCode Firstãåéã§ããããã«ãªããŸããã
ã¹ãã¬ããã·ãŒãã¹ããŒããšå€å¯Ÿå€ã®é¢ä¿ã®å®çŸ©
å¿ èŠã«å¿ããŠãçžäºã«æãããããã£ã䜿çšããŠå€å¯Ÿå€ã®é¢ä¿ãç°¡åã«å®çŸ©ã§ããŸãã ããšãã°ãæçš¿å ã®ã¿ã°ã远跡ããããã«ã¢ãã«ã«Tagã¯ã©ã¹ãè¿œå ããå Žåããããã®éã«å€å¯Ÿå€ã®é¢ä¿ãå¿ èŠã«ãªããŸãã
æ°ããã¿ã°ã¯ã©ã¹ïŒ
public class Tag { public int TagId{ get; set; } public string Name { get; set; } public ICollection<Post> Posts { get; set; } }
Postã¯ã©ã¹ã«è¿œå ããæ°ããããããã£ïŒ
public ICollection<Tag> Tags { get; set; }
ã³ãŒãã¯ãŸããã¹ããã³ã°ããŒãã«ã®ååã2ã€ã®ã¯ã©ã¹ã®ååã®çµã¿åããã§æ§æãããããŒãã«ã«ã¯ã©ã¹åãšããŒåã®çµã¿åããã§æ§æãããååã®å€éšããŒãå«ãŸããããšãåæãšããŠããŸãã ãã®å ŽåãPost.Idããã³Tag.TagIdã䜿çšããŠããŸãã Codeã«æåã«ããã«ãŠã§ã¢ããŒãã«ãäœæããããšã次ã®ããã«ãªããŸãã
ãŸããã³ãŒããæåã«èªåã§ããŒã¿ããŒã¹ãäœæã§ããããã«ããå ŽåãååãšããŠåé¡ã¯çºçããŸããã ãã ããæ¢åã®ããŒã¿ããŒã¹ã«ãããã³ã°ããå Žåã¯ãããŒãã«ãåã®ååã«åé¡ãããå¯èœæ§ããããŸãã Fluent APIã䜿çšããŠãããŒãã«åãšååãæå®ããŸãã
3ã€ã®ååãæå®ããå¿ èŠãããå Žåã®äŸãèããŠã¿ãŸãããã ããŒãã«åã¯PostJoinTagã§ãªããã°ãªãããååã¯TagIdãšPostIdã§ãªããã°ãªããŸããã
ãšã³ãã£ãã£ãããã³ã°ã¡ãœããããå§ããŸãããã æåã®æçš¿ãŸãã¯ã¿ã°ã®èª¬æã¯éèŠã§ã¯ãããŸããã 転èšãéžæããŸãã é¢ä¿ã®äž¡ç«¯ã瀺ããŸãã åã®äŸã§1察å€ã®é¢ä¿ã瀺ãããããã«ãHasManyã¡ãœãããšWithManyã¡ãœããã䜿çšããŠåãããšãè¡ããŸãã Postãšã³ãã£ãã£ã¯Tagsããããã£ãšè€æ°ã®é¢ä¿ãæã¡ãTagãšã³ãã£ãã£ã¯Postsããããã£ãšè€æ°ã®é¢ä¿ãæã€ããšãææããŸãã
modelBuilder.Entity<Post>() .HasMany(p => p.Tags) .WithMany(t => t.Posts) .Map(mc => { mc.ToTable("PostJoinTag"); mc.MapLeftKey("PostId"); mc.MapRightKey("TagId"); });
å·Šå³ã®ãããã³ã°ããŒïŒMapLeftKeyãMapRightKeyïŒã決å®ããéã«ã¯æ³šæãå¿ èŠã§ãã å·ŠåŽã¯ãããªããæã瀺ããæåã®ã¯ã©ã¹ã®ããŒãã€ãŸãPostã§ããå³åŽã®ããŒã¯2çªç®ã®ã¯ã©ã¹ã§ãã ãããã亀æãããšãããŒã¿ã¯æ£ããä¿åããããæ¢ç¥ã®ãã€ãã¹ã®çµæã«ã€ãªãããŸãã
çµè«
Code First fluent APIã䜿çšããŠé¢ä¿ãèšè¿°ããæ©èœã®ããã€ãã«ç²ŸéããŸããã åå¥ã«ããŸãã¯çµã¿åãããŠäœ¿çšââã§ããä»ã®ãããã³ã°ããããŸãã IsRequiredãšHasRequiredãŸãã¯WithManyãšHasManyãªã©ãäžéšã¯åé·ãŸãã¯æ··ä¹±ããŠããããã«èŠããå ŽåããããŸãã ãã ããåã¿ã¹ã¯ãæ åœããã¿ã¹ã¯ãšããããã䜿çšããå©ç¹ã確èªããŠããå¿ èŠããããŸãã
Entity Frameworkã®è©³çŽ°ã«ã€ããŠã¯ã MSDNãŸãã¯Entity FrameworkããŒã ã®ããã°ãã芧ãã ãã ã