{"id":255,"date":"2020-10-06T14:57:10","date_gmt":"2020-10-06T05:57:10","guid":{"rendered":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=255"},"modified":"2022-07-19T16:14:16","modified_gmt":"2022-07-19T07:14:16","slug":"ef-core%e3%81%ab%e3%82%88%e3%82%8bdb%e3%81%8b%e3%82%89%e3%81%aeentity%e4%bd%9c%e6%88%90scaffolding","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=255","title":{"rendered":"EF Core\u306b\u3088\u308bDB\u304b\u3089\u306eEntity\u4f5c\u6210(Scaffolding)"},"content":{"rendered":"\n<p>\u4ee5\u524d\u3001\u5c11\u3057\u66f8\u3044\u305f\u3068\u601d\u3046\u304c\u3001Entity Framework Core\u3092\u4f7f\u7528\u3057\u3066\u3001\u65e2\u5b58\u306eDB\u304b\u3089DbContext\u30af\u30e9\u30b9\u3068Entity\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u3067\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u3092\u542b\u3080DB\u304c\u3042\u3063\u305f\u5834\u5408\u3001\u3069\u306e\u3088\u3046\u306aEntity\u304a\u3088\u3073DbContext\u3068\u306a\u308b\u304b\u8a66\u3057\u3066\u307f\u3088\u3046\u3002<\/p>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9(SQLite)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-sql\" data-file=\"\u30c6\u30fc\u30d6\u30eb\" data-lang=\"SQL\"><code>CREATE TABLE Person(\n  Id int atuoincrement,\n  Name nvarchar(50),\n  primary key (Id)\n);\nCREATE TABLE MailAddress(\n  Address nvarchar(100),\n  DisplayName nvarchar(50),\n  PersonId int,\n  primary key(Address),\n  foreign key(PersonId) references Person(Id)\n);<\/code><\/pre><\/div>\n\n\n\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u306f\u4e0b\u8a18\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8ffd\u52a0\u3059\u308b<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microsoft.EntityFrameworkCore<\/li><li>Microsoft.EntityFrameoworkCore.Design<\/li><li>Microsoft.EntityFrameworkCore.Sqlite<\/li><li><s>Microsoft.EntityFrameowrkCore.Sqlite.Design<\/s>(\u8981\u3089\u306a\u304f\u306a\u3063\u305f\u3088\u3046\u3060)<\/li><\/ul>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001DbContext\u3068Entity\u3092\u4f5c\u6210<br>(scaffold\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u300c\u63a5\u7d9a\u6587\u5b57\u5217\u300d\u3068\u300c\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0\u300d)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-bash\" data-file=\"Scaffold\" data-lang=\"Bash\"><code>$ dotnet ef dbcontext scaffold &quot;Data Source=SQLiteTest.db&quot; Microsoft.EntityFrameworkCore.Sqlite<\/code><\/pre><\/div>\n\n\n\n<p>\u51fa\u6765\u4e0a\u304c\u3063\u305fDbContext\u3068Entity<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"SQLiteTestContext.cs\" data-lang=\"C#\"><code>using System;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Metadata;\n\nnamespace SQLiteTest\n{\n    public partial class SQLiteTestContext : DbContext\n    {\n        public SQLiteTestContext()\n        {\n        }\n\n        public SQLiteTestContext(DbContextOptions&lt;SQLiteTestContext&gt; options)\n            : base(options)\n        {\n        }\n\n        public virtual DbSet&lt;MailAddress&gt; MailAddress { get; set; }\n        public virtual DbSet&lt;Person&gt; Person { get; set; }\n\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n        {\n            if (!optionsBuilder.IsConfigured)\n            {\n#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http:\/\/go.microsoft.com\/fwlink\/?LinkId=723263 for guidance on storing connection strings.\n                optionsBuilder.UseSqlite(&quot;Data Source=SQLiteTest.db&quot;);\n            }\n        }\n\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\n        {\n            modelBuilder.Entity&lt;MailAddress&gt;(entity =&gt;\n            {\n                entity.HasKey(e =&gt; e.Address);\n\n                entity.Property(e =&gt; e.Address).HasColumnType(&quot;nvarchar(100)&quot;);\n\n                entity.Property(e =&gt; e.DisplayName).HasColumnType(&quot;nvarchar(50)&quot;);\n\n                entity.Property(e =&gt; e.PersonId).HasColumnType(&quot;int&quot;);\n\n                entity.HasOne(d =&gt; d.Person)\n                    .WithMany(p =&gt; p.MailAddress)\n                    .HasForeignKey(d =&gt; d.PersonId);\n            });\n\n            modelBuilder.Entity&lt;Person&gt;(entity =&gt;\n            {\n                entity.Property(e =&gt; e.Id)\n                    .HasColumnType(&quot;int atuoincrement&quot;)\n                    .ValueGeneratedNever();\n\n                entity.Property(e =&gt; e.Name).HasColumnType(&quot;nvarchar(50)&quot;);\n            });\n\n            OnModelCreatingPartial(modelBuilder);\n        }\n\n        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"Person.cs\" data-lang=\"C#\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace SQLiteTest\n{\n    public partial class Person\n    {\n        public Person()\n        {\n            MailAddress = new HashSet&lt;MailAddress&gt;();\n        }\n\n        public long Id { get; set; }\n        public string Name { get; set; }\n\n        public virtual ICollection&lt;MailAddress&gt; MailAddress { get; set; }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"MailAddress.cs\" data-lang=\"C#\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace SQLiteTest\n{\n    public partial class MailAddress\n    {\n        public string Address { get; set; }\n        public string DisplayName { get; set; }\n        public long? PersonId { get; set; }\n\n        public virtual Person Person { get; set; }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u3068\u307e\u3042\u3001\u3053\u3093\u306a\u611f\u3058\u3067DB\u304b\u3089DbContext\u3068Entity\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br>\u63a5\u7d9a\u6587\u5b57\u5217\u304c\u57cb\u3081\u8fbc\u307f\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u3053\u306e\u90e8\u5206\u306fappsettings.json\u7b49\u304b\u3089\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u305f\u65b9\u304c\u826f\u3044\u3060\u308d\u3046\u3002<\/p>\n\n\n\n<p>\u306a\u304a\u3001scaffold\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3001scaffold\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3092\u6307\u5b9a\u3057\u305f\u308a\u3001\u51fa\u529b\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u3001\u5c11\u3057\u66f8\u3044\u305f\u3068\u601d\u3046\u304c\u3001Entity Framework Core\u3092\u4f7f\u7528\u3057\u3066\u3001 &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=255\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11,14,4],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-c","category-dotnetcore","category-entity-framework","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255"}],"version-history":[{"count":7,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}