"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[7300],{6055:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>a,default:()=>h,frontMatter:()=>s,metadata:()=>c,toc:()=>d});var t=i(5893),r=i(1151),o=i(4996);const s={id:"dbcontext-code-first",title:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93",sidebar_label:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93 (Code First)"},a=void 0,c={id:"dbcontext-code-first",title:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93",description:"Fur.Web.Entry \u5c42\u9700\u8981\u5b89\u88c5 Microsoft.EntityFrameworkCore.Tools \u5305\u3002",source:"@site/docs/dbcontext-code-first.mdx",sourceDirName:".",slug:"/dbcontext-code-first",permalink:"/docs/dbcontext-code-first",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/dbcontext-code-first.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1682389320,formattedLastUpdatedAt:"Apr 25, 2023",frontMatter:{id:"dbcontext-code-first",title:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93",sidebar_label:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93 (Code First)"},sidebar:"docs",previous:{title:"9.20 \u6570\u636e\u5e93\u751f\u6210\u6a21\u578b (Db First)",permalink:"/docs/dbcontext-db-first"},next:{title:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e",permalink:"/docs/dbcontext-seed-data"}},l={},d=[{value:"9.21.1 \u6570\u636e\u5e93\u5f00\u53d1\u65b9\u5f0f",id:"9211-\u6570\u636e\u5e93\u5f00\u53d1\u65b9\u5f0f",level:2},{value:"9.21.2 \u64cd\u4f5c\u6307\u5357",id:"9212-\u64cd\u4f5c\u6307\u5357",level:2},{value:"9.21.2.1 \u521b\u5efa\u5b9e\u4f53\u6a21\u578b Person
",id:"92121-\u521b\u5efa\u5b9e\u4f53\u6a21\u578b-person",level:3},{value:"9.21.2.2 \u6253\u5f00 \u7a0b\u5e8f\u5305\u7ba1\u7406\u63a7\u5236\u53f0
",id:"92122-\u6253\u5f00-\u7a0b\u5e8f\u5305\u7ba1\u7406\u63a7\u5236\u53f0",level:3},{value:"9.21.2.3 \u5207\u6362\u9ed8\u8ba4\u9879\u76ee",id:"92123-\u5207\u6362\u9ed8\u8ba4\u9879\u76ee",level:3},{value:"9.21.2.4 \u521b\u5efa\u6a21\u578b\u7248\u672c",id:"92124-\u521b\u5efa\u6a21\u578b\u7248\u672c",level:3},{value:"9.21.2.5 \u66f4\u65b0\u5230\u6570\u636e\u5e93",id:"92125-\u66f4\u65b0\u5230\u6570\u636e\u5e93",level:3},{value:"9.21.3 \u66f4\u65b0\u6a21\u578b",id:"9213-\u66f4\u65b0\u6a21\u578b",level:2},{value:"9.21.4 \u5bfc\u51fa Sql
",id:"9214-\u5bfc\u51fa-sql",level:2},{value:"9.21.5 VS Code/Rider/\u4efb\u4f55IDE/\u64cd\u4f5c\u7cfb\u7edf
\u65b9\u5f0f",id:"9215-vs-coderider\u4efb\u4f55ide\u64cd\u4f5c\u7cfb\u7edf-\u65b9\u5f0f",level:2},{value:"9.21.5.1 \u5b89\u88c5 dotnet ef
",id:"92151-\u5b89\u88c5-dotnet-ef",level:3},{value:"9.21.5.2 cd
\u76ee\u5f55",id:"92152-cd-\u76ee\u5f55",level:3},{value:"9.21.5.3 \u6267\u884c\u547d\u4ee4",id:"92153-\u6267\u884c\u547d\u4ee4",level:3},{value:"9.21.6 \u5e94\u7528\u542f\u52a8\u65f6\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93",id:"9216-\u5e94\u7528\u542f\u52a8\u65f6\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93",level:2},{value:"9.21.6.1 \u5bf9\u5df2\u7ecf\u751f\u6210 Migrations
\u6587\u4ef6\u60c5\u51b5",id:"92161-\u5bf9\u5df2\u7ecf\u751f\u6210-migrations-\u6587\u4ef6\u60c5\u51b5",level:3},{value:"9.21.6.2 \u5982\u679c\u6ca1\u6709\u751f\u6210\u8fc7 Migrations
\u6587\u4ef6\u60c5\u51b5",id:"92162-\u5982\u679c\u6ca1\u6709\u751f\u6210\u8fc7-migrations-\u6587\u4ef6\u60c5\u51b5",level:3},{value:"9.21.7 MySql.EntityFrameworkCore
\u5728 .NET 6.0.8+
\u95ee\u9898",id:"9217-mysqlentityframeworkcore-\u5728-net-608-\u95ee\u9898",level:2},{value:"9.21.8 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"9218-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function p(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.admonition,{title:"\u5f00\u59cb\u4e4b\u524d",type:"tip",children:(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Fur.Web.Entry"})," \u5c42\u9700\u8981\u5b89\u88c5 ",(0,t.jsx)(n.code,{children:"Microsoft.EntityFrameworkCore.Tools"})," \u5305\u3002"]})}),"\n",(0,t.jsx)(n.h2,{id:"9211-\u6570\u636e\u5e93\u5f00\u53d1\u65b9\u5f0f",children:"9.21.1 \u6570\u636e\u5e93\u5f00\u53d1\u65b9\u5f0f"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Furion"})," \u63d0\u4f9b\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u6765 ",(0,t.jsx)(n.strong,{children:"\u4fdd\u6301\u5b9e\u4f53\u6a21\u578b\u548c\u6570\u636e\u5e93\u67b6\u6784\u540c\u6b65"}),"\u3002"]}),"\n",(0,t.jsx)(n.p,{children:"\u81f3\u4e8e\u6211\u4eec\u5e94\u8be5\u9009\u7528\u54ea\u4e2a\u65b9\u6cd5\uff0c\u8bf7\u786e\u5b9a\u4f60\u662f\u5e0c\u671b\u4ee5\u5b9e\u4f53\u6a21\u578b\u4e3a\u51c6\u8fd8\u662f\u4ee5\u6570\u636e\u5e93\u4e3a\u51c6\uff1a"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:["\u5982\u679c\u5e0c\u671b ",(0,t.jsx)(n.strong,{children:"\u4ee5\u5b9e\u4f53\u6a21\u578b\u4e3a\u51c6"}),"\uff0c\u8bf7\u4f7f\u7528\u6b63\u5411\u5de5\u7a0b\uff08Code First\uff09\u3002 \u5bf9\u5b9e\u4f53\u6a21\u578b\u8fdb\u884c\u66f4\u6539\u65f6\uff0c\u6b64\u65b9\u6cd5\u4f1a\u4ee5\u589e\u91cf\u65b9\u5f0f\u5c06\u76f8\u5e94\u67b6\u6784\u66f4\u6539\u5e94\u7528\u5230\u6570\u636e\u5e93\uff0c\u4ee5\u4f7f\u6570\u636e\u5e93\u4fdd\u6301\u4e0e\u5b9e\u4f53\u6a21\u578b\u517c\u5bb9\u3002"]}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:["\u5982\u679c\u5e0c\u671b ",(0,t.jsx)(n.strong,{children:"\u4ee5\u6570\u636e\u5e93\u67b6\u6784\u4e3a\u51c6"}),"\uff0c\u8bf7\u4f7f\u7528\u53cd\u5411\u5de5\u7a0b\uff08Database First\uff09\u3002 \u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u53ef\u901a\u8fc7\u5c06\u6570\u636e\u5e93\u67b6\u6784\u53cd\u5411\u5de5\u7a0b\u5230\u5b9e\u4f53\u6a21\u578b\u6765\u751f\u6210\u76f8\u5e94\u7684\u5b9e\u4f53\u7c7b\u578b\u3002"]}),"\n"]}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["\u672c\u7ae0\u8282\u662f ",(0,t.jsx)(n.strong,{children:"\u6b63\u5411\u5de5\u7a0b\uff08Code First\uff09"})," \u7684\u76f8\u5173\u5185\u5bb9\u3002"]}),"\n",(0,t.jsx)(n.h2,{id:"9212-\u64cd\u4f5c\u6307\u5357",children:"9.21.2 \u64cd\u4f5c\u6307\u5357"}),"\n",(0,t.jsxs)(n.admonition,{title:"\u7279\u522b\u6ce8\u610f",type:"important",children:[(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u9ed8\u8ba4\u6570\u636e\u8fc1\u79fb\u7684\u7a0b\u5e8f\u96c6\u4e3a\uff1a",(0,t.jsx)(n.code,{children:"Furion.Database.Migrations"}),"\uff0c\u6240\u4ee5\u5982\u679c\u60a8\u6539\u4e86\u7a0b\u5e8f\u96c6\u540d\u79f0\u6216\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"NuGet"})," \u65b9\u5f0f\u5b89\u88c5\u7684 ",(0,t.jsx)(n.code,{children:"Furion"})," \u5305\uff0c\u5219\u9700\u8981\u914d\u7f6e\u8fc1\u79fb\u7a0b\u5e8f\u96c6\u540d\u79f0\uff1a"]}),(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {4}",children:'services.AddDatabaseAccessor(options =>\r\n{\r\n options.AddDbPool(DbProvider.Sqlite);\r\n}, "\u5b58\u653e\u8fc1\u79fb\u6587\u4ef6\u7684\u9879\u76ee\u540d\u79f0");\n'})}),(0,t.jsxs)(n.p,{children:["\u53e6\u5916\uff0c\u5982\u679c\u5e94\u7528\u4e2d\u914d\u7f6e\u4e86\u591a\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\uff0c\u90a3\u4e48\u6240\u6709\u7684 ",(0,t.jsx)(n.code,{children:"\u8fc1\u79fb\u547d\u4ee4"})," \u90fd\u9700\u8981\u6307\u5b9a ",(0,t.jsx)(n.code,{children:"-Context \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u540d\u79f0"})," \u53c2\u6570\u3002\u5982\uff1a"]}),(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Add-Migration v1.0.0 -Context FurionDbContext\n"})})]}),"\n",(0,t.jsxs)(n.h3,{id:"92121-\u521b\u5efa\u5b9e\u4f53\u6a21\u578b-person",children:["9.21.2.1 \u521b\u5efa\u5b9e\u4f53\u6a21\u578b ",(0,t.jsx)(n.code,{children:"Person"})]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,6,8}",children:"using Furion.DatabaseAccessor;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel.DataAnnotations;\r\n\r\nnamespace Furion.Core\r\n{\r\n public class Person : Entity\r\n {\r\n /// \r\n /// \u6784\u9020\u51fd\u6570\r\n /// \r\n public Person()\r\n {\r\n CreatedTime = DateTime.Now;\r\n IsDeleted = false;\r\n }\r\n\r\n /// \r\n /// \u59d3\u540d\r\n /// \r\n [MaxLength(32)]\r\n public string Name { get; set; }\r\n\r\n /// \r\n /// \u5e74\u9f84\r\n /// \r\n public int Age { get; set; }\r\n\r\n /// \r\n /// \u4f4f\u5740\r\n /// \r\n public string Address { get; set; }\r\n }\r\n}\n"})}),"\n",(0,t.jsx)(n.admonition,{title:"\u5b9e\u4f53\u7ea6\u5b9a",type:"important",children:(0,t.jsxs)(n.p,{children:["\u6240\u6709\u6570\u636e\u5e93\u5b9e\u4f53\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f ",(0,t.jsx)(n.code,{children:"IEntity"})," \u63a5\u53e3\u3002"]})}),"\n",(0,t.jsxs)(n.h3,{id:"92122-\u6253\u5f00-\u7a0b\u5e8f\u5305\u7ba1\u7406\u63a7\u5236\u53f0",children:["9.21.2.2 \u6253\u5f00 ",(0,t.jsx)(n.code,{children:"\u7a0b\u5e8f\u5305\u7ba1\u7406\u63a7\u5236\u53f0"})]}),"\n",(0,t.jsx)("img",{src:(0,o.Z)("img/dbfirst1.png")}),"\n",(0,t.jsx)(n.h3,{id:"92123-\u5207\u6362\u9ed8\u8ba4\u9879\u76ee",children:"9.21.2.3 \u5207\u6362\u9ed8\u8ba4\u9879\u76ee"}),"\n",(0,t.jsxs)(n.p,{children:["\u5c06 ",(0,t.jsx)(n.code,{children:"\u7a0b\u5e8f\u5305\u7ba1\u7406\u63a7\u5236\u53f0"})," \u9ed8\u8ba4\u9879\u76ee\u8bbe\u7f6e\u4e3a ",(0,t.jsx)(n.code,{children:"Furion.Database.Migrations"})]}),"\n",(0,t.jsx)("img",{src:(0,o.Z)("img/codefirst1.png")}),"\n",(0,t.jsx)(n.h3,{id:"92124-\u521b\u5efa\u6a21\u578b\u7248\u672c",children:"9.21.2.4 \u521b\u5efa\u6a21\u578b\u7248\u672c"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Add-Migration v1.0.0\n"})}),"\n",(0,t.jsx)(n.admonition,{title:"\u7279\u522b\u8bf4\u660e",type:"note",children:(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"v1.0.0"})," \u662f\u6b64\u5904\u6570\u636e\u5e93\u66f4\u6539\u7684\u7248\u672c\u53f7\uff0c\u53ef\u4ee5\u5199\u4efb\u4f55\u5b57\u7b26\u4e32\uff0c\u4f46\u63a8\u8350\u5199\u7248\u672c\u53f7\uff0c\u6bcf\u6b21 ",(0,t.jsx)(n.strong,{children:"+1"}),"\u3002"]})}),"\n",(0,t.jsx)(n.p,{children:"\u6700\u7ec8\u547d\u4ee4\u5982\u4e0b\uff1a"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"PM> Add-Migration v1.0.0\r\nBuild started...\r\nBuild succeeded.\r\nMicrosoft.EntityFrameworkCore.Model.Validation[10400]\r\n Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.\r\nMicrosoft.EntityFrameworkCore.Infrastructure[10403]\r\n Entity Framework Core 5.0.0-rc.1.20451.13 initialized 'FurionDbContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: SensitiveDataLoggingEnabled DetailedErrorsEnabled MaxPoolSize=100 MigrationsAssembly=Furion.Database.Migrations\r\nTo undo this action, use Remove-Migration.\r\nPM>\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u751f\u6210\u6210\u529f\u540e\uff0c",(0,t.jsx)(n.code,{children:"Furion.Database.Migrations"})," \u9879\u76ee\u4e0b\u4f1a\u65b0\u589e ",(0,t.jsx)(n.code,{children:"Migrations"})," \u6587\u4ef6\u5939\uff08\u5982\u679c\u6ca1\u6709\uff09\uff0c\u540c\u65f6\u672c\u6b21\u7684\u67b6\u6784\u751f\u6210\u6587\u4ef6\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)("img",{src:(0,o.Z)("img/codefirst3.png")}),"\n",(0,t.jsx)(n.h3,{id:"92125-\u66f4\u65b0\u5230\u6570\u636e\u5e93",children:"9.21.2.5 \u66f4\u65b0\u5230\u6570\u636e\u5e93"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Update-Database\n"})}),"\n",(0,t.jsx)(n.p,{children:"\u6267\u884c\u8be5\u547d\u4ee4\u540e\uff0c\u6570\u636e\u5e93\u5c31\u4f1a\u81ea\u52a8\u6839\u636e\u6a21\u578b\u751f\u6210\u5bf9\u5e94\u7684\u8868\u3002"}),"\n",(0,t.jsxs)(n.admonition,{title:"\u5c0f\u77e5\u8bc6",type:"tip",children:[(0,t.jsxs)(n.p,{children:["\u5982\u679c ",(0,t.jsx)(n.code,{children:"Update-Database"})," \u540e\u9762\u5e26\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5219\u4f1a\u81ea\u52a8\u8fd8\u539f\u6570\u636e\u5e93\u5230\u6307\u5b9a\u7248\u672c\uff0c\u5982\uff1a"]}),(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Update-Database v0.0.3\n"})}),(0,t.jsxs)(n.p,{children:["\u5c06\u6570\u636e\u5e93\u8fd8\u539f\u5230 ",(0,t.jsx)(n.code,{children:"v0.0.3"})," \u7248\u672c"]})]}),"\n",(0,t.jsx)(n.h2,{id:"9213-\u66f4\u65b0\u6a21\u578b",children:"9.21.3 \u66f4\u65b0\u6a21\u578b"}),"\n",(0,t.jsx)(n.p,{children:"\u5982\u679c\u6a21\u578b\u6539\u53d8\u4e86\uff0c\u91cd\u590d\u4e0a\u9762\u64cd\u4f5c\u5373\u53ef\uff0c\u5982\uff1a"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Add-Migration v1.0.1\n"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Update-Database\n"})}),"\n",(0,t.jsxs)(n.h2,{id:"9214-\u5bfc\u51fa-sql",children:["9.21.4 \u5bfc\u51fa ",(0,t.jsx)(n.code,{children:"Sql"})]}),"\n",(0,t.jsxs)(n.p,{children:["\u6709\u4e9b\u65f6\u5019\uff0c\u6211\u4eec\u6ca1\u6709\u76f4\u63a5\u66f4\u65b0\u6570\u636e\u5e93\u7684\u6743\u9650\uff0c\u6216\u8005\u6015\u51fa\u95ee\u9898\uff0c\u6211\u4eec\u90fd\u4f1a\u5148\u751f\u6210 ",(0,t.jsx)(n.code,{children:"Sql"})," \u770b\u770b\uff0c\u8fd9\u65f6\u5019\u53ea\u9700\u8981\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"Script-Migration"})," \u5bfc\u51fa\u5373\u53ef\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"Script-Migration\n"})}),"\n",(0,t.jsx)("img",{src:(0,o.Z)("img/codefirst2.png")}),"\n",(0,t.jsxs)(n.h2,{id:"9215-vs-coderider\u4efb\u4f55ide\u64cd\u4f5c\u7cfb\u7edf-\u65b9\u5f0f",children:["9.21.5 ",(0,t.jsx)(n.code,{children:"VS Code/Rider/\u4efb\u4f55IDE/\u64cd\u4f5c\u7cfb\u7edf"})," \u65b9\u5f0f"]}),"\n",(0,t.jsxs)(n.h3,{id:"92151-\u5b89\u88c5-dotnet-ef",children:["9.21.5.1 \u5b89\u88c5 ",(0,t.jsx)(n.code,{children:"dotnet ef"})]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"dotnet tool install --global dotnet-ef --version 5.0.0-rc.2.20475.6\n"})}),"\n",(0,t.jsxs)(n.h3,{id:"92152-cd-\u76ee\u5f55",children:["9.21.5.2 ",(0,t.jsx)(n.code,{children:"cd"})," \u76ee\u5f55"]}),"\n",(0,t.jsxs)(n.p,{children:["\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"VS Code"})," \u6253\u5f00 ",(0,t.jsx)(n.code,{children:".sln"})," \u6240\u5728\u7684\u76ee\u5f55\uff0c\u5982\uff1a",(0,t.jsx)(n.code,{children:"framework"}),"\u3002"]}),"\n",(0,t.jsxs)(n.p,{children:["\u4e4b\u540e\u8fdb\u5165 ",(0,t.jsx)(n.code,{children:"Furion.Database.Migrations"})," \u76ee\u5f55"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:"cd Furion.Database.Migrations\n"})}),"\n",(0,t.jsx)(n.h3,{id:"92153-\u6267\u884c\u547d\u4ee4",children:"9.21.5.3 \u6267\u884c\u547d\u4ee4"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:'dotnet ef migrations add v1.0.0 -s "../Furion.Web.Entry"\n'})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-shell",metastring:"showLineNumbers",children:'dotnet ef database update -s "../Furion.Web.Entry"\n'})}),"\n",(0,t.jsx)(n.h2,{id:"9216-\u5e94\u7528\u542f\u52a8\u65f6\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93",children:"9.21.6 \u5e94\u7528\u542f\u52a8\u65f6\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528\u547d\u4ee4\u65b9\u5f0f\u64cd\u4f5c\u6570\u636e\u5e93\uff0c",(0,t.jsx)(n.strong,{children:"\u5b8c\u5168\u4e0d\u63a8\u8350\u81ea\u52a8\u5316\u751f\u6210\u6570\u636e\u5e93"}),"\uff0c\u4f46\u662f\u6709\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6709\u8fd9\u4e2a\u5fc5\u8981\uff0c\u6545\u5c06\u6b64\u529f\u80fd\u5199\u51fa\uff1a"]}),"\n",(0,t.jsxs)(n.h3,{id:"92161-\u5bf9\u5df2\u7ecf\u751f\u6210-migrations-\u6587\u4ef6\u60c5\u51b5",children:["9.21.6.1 \u5bf9\u5df2\u7ecf\u751f\u6210 ",(0,t.jsx)(n.code,{children:"Migrations"})," \u6587\u4ef6\u60c5\u51b5"]}),"\n",(0,t.jsxs)(n.p,{children:["\u5982\u679c\u5df2\u7ecf\u751f\u6210 ",(0,t.jsx)(n.code,{children:"Migrations"})," \u6587\u4ef6\uff0c\u90a3\u4e48\u53ef\u4ee5\u76f4\u63a5\u5728 ",(0,t.jsx)(n.code,{children:"Startup.cs"})," \u4ee3\u7801\u4e2d\u5b9e\u73b0\u7a0b\u5e8f\u542f\u52a8\u65f6\u81ea\u52a8\u6267\u884c ",(0,t.jsx)(n.code,{children:"update-database"})," \u547d\u4ee4\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4,6,8-9}",children:"public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n{\r\n // \u5224\u65ad\u5f00\u53d1\u73af\u5883\uff01\uff01\uff01\u5fc5\u987b\uff01\uff01\uff01\uff01\r\n if (env.IsDevelopment())\r\n {\r\n Scoped.Create((_, scope) =>\r\n {\r\n var context = scope.ServiceProvider.GetRequiredService();\r\n context.Database.Migrate();\r\n });\r\n }\r\n\r\n // \u5176\u4ed6\u4ee3\u7801\r\n}\n"})}),"\n",(0,t.jsxs)(n.h3,{id:"92162-\u5982\u679c\u6ca1\u6709\u751f\u6210\u8fc7-migrations-\u6587\u4ef6\u60c5\u51b5",children:["9.21.6.2 \u5982\u679c\u6ca1\u6709\u751f\u6210\u8fc7 ",(0,t.jsx)(n.code,{children:"Migrations"})," \u6587\u4ef6\u60c5\u51b5"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4,6,8-9}",children:"public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n{\r\n // \u5224\u65ad\u5f00\u53d1\u73af\u5883\uff01\uff01\uff01\u5fc5\u987b\uff01\uff01\uff01\uff01\r\n if (env.IsDevelopment())\r\n {\r\n Scoped.Create((_, scope) =>\r\n {\r\n var context = scope.ServiceProvider.GetRequiredService();\r\n context.Database.EnsureCreated();\r\n });\r\n }\r\n\r\n // \u5176\u4ed6\u4ee3\u7801\r\n}\n"})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsxs)(n.strong,{children:["\u5982\u679c\u9700\u8981\u5728\u521b\u5efa\u6570\u636e\u5e93\u4e4b\u524d\u5148\u5220\u9664\u65e7\u7684\uff0c\u53ef\u5148\u8c03\u7528 ",(0,t.jsx)(n.code,{children:"context.Database.EnsureDeleted();"})," \u4ee3\u7801\u3002\u614e\u91cd\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01"]})}),"\n",(0,t.jsxs)(n.h2,{id:"9217-mysqlentityframeworkcore-\u5728-net-608-\u95ee\u9898",children:["9.21.7 ",(0,t.jsx)(n.code,{children:"MySql.EntityFrameworkCore"})," \u5728 ",(0,t.jsx)(n.code,{children:".NET 6.0.8+"})," \u95ee\u9898"]}),"\n",(0,t.jsxs)(n.p,{children:["\u5728 ",(0,t.jsx)(n.code,{children:".NET 6.0.8+"})," \u7248\u672c\uff0c\u5fae\u8f6f\u5e95\u5c42\u4fee\u6539\u4e86 ",(0,t.jsx)(n.code,{children:"IDesignTimeServices"})," \u903b\u8f91\u5bfc\u81f4 ",(0,t.jsx)(n.code,{children:"MySql.EntityFrameworkCore"})," \u7248\u672c\u6ca1\u6709\u53ca\u65f6\u66f4\u65b0\u5bfc\u81f4\u4e00\u4e0b\u9519\u8bef\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",metastring:"showLineNumbers {51}",children:"PM> Add-Migration v0.0.1\r\nBuild started...\r\nBuild succeeded.\r\nMicrosoft.EntityFrameworkCore.Model.Validation[10400]\r\n Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.\r\nMicrosoft.EntityFrameworkCore.Infrastructure[10403]\r\n Entity Framework Core 6.0.8 initialized 'DefaultDbContext' using provider 'MySql.EntityFrameworkCore:6.0.4+MySQL8.0.30' with options: SensitiveDataLoggingEnabled DetailedErrorsEnabled MaxPoolSize=100 MigrationsAssembly=Furion.TestMS.Database.Migrations\r\nSystem.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies' while attempting to activate 'MySql.EntityFrameworkCore.Storage.Internal.MySQLTypeMappingSource'.\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateEnumerable(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)\r\n at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)\r\n at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)\r\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)\r\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()\r\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()\r\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)\r\nUnable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies' while attempting to activate 'MySql.EntityFrameworkCore.Storage.Internal.MySQLTypeMappingSource'.\r\nPM>\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u89e3\u51b3\u529e\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u542f\u52a8\u5c42\u6dfb\u52a0 ",(0,t.jsx)(n.code,{children:"MysqlEntityFrameworkDesignTimeServices.cs"})," \u5e76\u5199\u5165\u4ee5\u4e0b\u5185\u5bb9\u5373\u53ef\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {4,6}",children:"using Microsoft.EntityFrameworkCore.Design;\r\nusing MySql.EntityFrameworkCore.Extensions;\r\n\r\nnamespace YourProject.Web.Entry;\r\n\r\npublic class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices\r\n{\r\n public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)\r\n {\r\n serviceCollection.AddEntityFrameworkMySQL();\r\n new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)\r\n .TryAddCoreServices();\r\n }\r\n}\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u76f8\u5173 ",(0,t.jsx)(n.code,{children:"Issue"})," \u8ba8\u8bba\uff1a",(0,t.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/I5O5ER",children:"https://gitee.com/dotnetchina/Furion/issues/I5O5ER"})]}),"\n",(0,t.jsx)(n.h2,{id:"9218-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"9.21.8 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,t.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,t.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,t.jsx)(n.hr,{}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,t.jsxs)(n.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,t.jsx)(n.code,{children:"\u6b63\u5411\u5de5\u5382"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,t.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli",children:"EF Core - \u7ba1\u7406\u6570\u636e\u5e93\u67b6\u6784"})," \u7ae0\u8282\u3002"]})})]})}function h(e={}){const{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(p,{...e})}):p(e)}},1151:(e,n,i)=>{i.d(n,{Z:()=>a,a:()=>s});var t=i(7294);const r={},o=t.createContext(r);function s(e){const n=t.useContext(o);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),t.createElement(o.Provider,{value:n},e.children)}}}]);