"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[3942],{4733:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>d,default:()=>u,frontMatter:()=>i,metadata:()=>o,toc:()=>c});var r=t(5893),s=t(1151);const i={id:"dbcontext-seed-data",title:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e",sidebar_label:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e"},d=void 0,o={id:"dbcontext-seed-data",title:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e",description:"\u4e00\u65e6\u5b9a\u4e49\u4e86\u79cd\u5b50\u6570\u636e\u6216\u6539\u53d8\u4e86\u79cd\u5b50\u6570\u636e\uff0c\u9700\u8981\u91cd\u65b0\u6267\u884c Add-Migration \u548c Update-Database \u547d\u4ee4\u3002",source:"@site/docs/dbcontext-seed-data.mdx",sourceDirName:".",slug:"/dbcontext-seed-data",permalink:"/docs/dbcontext-seed-data",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/dbcontext-seed-data.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1682389320,formattedLastUpdatedAt:"Apr 25, 2023",frontMatter:{id:"dbcontext-seed-data",title:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e",sidebar_label:"9.22 \u5b9e\u4f53\u79cd\u5b50\u6570\u636e"},sidebar:"docs",previous:{title:"9.21 \u6a21\u578b\u751f\u6210\u6570\u636e\u5e93 (Code First)",permalink:"/docs/dbcontext-code-first"},next:{title:"9.23 \u5ba1\u8ba1\u65e5\u5fd7 (Audit)",permalink:"/docs/dbcontext-audit"}},a={},c=[{value:"9.22.1 \u4ec0\u4e48\u662f\u79cd\u5b50\u6570\u636e",id:"9221-\u4ec0\u4e48\u662f\u79cd\u5b50\u6570\u636e",level:2},{value:"9.22.2 \u5982\u4f55\u914d\u7f6e",id:"9222-\u5982\u4f55\u914d\u7f6e",level:2},{value:"9.22.2.1 \u5728\u5b9e\u4f53\u5b9a\u4e49\u4e2d\u4f7f\u7528",id:"92221-\u5728\u5b9e\u4f53\u5b9a\u4e49\u4e2d\u4f7f\u7528",level:3},{value:"9.22.2.2 \u5728\u4efb\u610f\u5bf9\u8c61\u7c7b\u4e2d\u4f7f\u7528",id:"92222-\u5728\u4efb\u610f\u5bf9\u8c61\u7c7b\u4e2d\u4f7f\u7528",level:3},{value:"9.22.3 \u5bfc\u822a\u5c5e\u6027",id:"9223-\u5bfc\u822a\u5c5e\u6027",level:2},{value:"9.22.4 \u591a\u4e2a\u6570\u636e\u5e93\u79cd\u5b50\u6570\u636e",id:"9224-\u591a\u4e2a\u6570\u636e\u5e93\u79cd\u5b50\u6570\u636e",level:2},{value:"9.22.5 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"9225-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function l(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",p:"p",pre:"pre",strong:"strong",...(0,s.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.admonition,{title:"\u7279\u522b\u63d0\u9192",type:"important",children:(0,r.jsxs)(n.p,{children:["\u4e00\u65e6\u5b9a\u4e49\u4e86\u79cd\u5b50\u6570\u636e\u6216\u6539\u53d8\u4e86\u79cd\u5b50\u6570\u636e\uff0c\u9700\u8981\u91cd\u65b0\u6267\u884c ",(0,r.jsx)(n.code,{children:"Add-Migration"})," \u548c ",(0,r.jsx)(n.code,{children:"Update-Database"})," \u547d\u4ee4\u3002"]})}),"\n",(0,r.jsx)(n.h2,{id:"9221-\u4ec0\u4e48\u662f\u79cd\u5b50\u6570\u636e",children:"9.22.1 \u4ec0\u4e48\u662f\u79cd\u5b50\u6570\u636e"}),"\n",(0,r.jsxs)(n.p,{children:["\u5728 ",(0,r.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\uff0c\u79cd\u5b50\u6570\u636e\u901a\u5e38\u6307\u7684\u662f\u901a\u8fc7\u7a0b\u5e8f\u4e3a\u6570\u636e\u5e93\u9884\u5148\u8bbe\u7f6e\u4e00\u4e9b\u521d\u59cb\u5316\u6570\u636e\uff0c\u6bd4\u5982\u6211\u4eec\u7684\u6570\u636e\u5b57\u5178\u8868\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5728\u7cfb\u7edf\u6784\u5efa\u521d\u671f\u5c31\u81ea\u52a8\u5c06\u4e00\u4e9b\u89c4\u8303\u5316\u6570\u636e\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002"]}),"\n",(0,r.jsx)(n.p,{children:"\u5982\u6027\u522b\uff1a\u7537/\u5973\uff0c\u5730\u533a\uff0c\u884c\u4e1a\u4fe1\u606f\u7b49\u7b49\u3002"}),"\n",(0,r.jsx)(n.h2,{id:"9222-\u5982\u4f55\u914d\u7f6e",children:"9.22.2 \u5982\u4f55\u914d\u7f6e"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"Furion"})," \u63d0\u4f9b\u975e\u5e38\u7075\u6d3b\u65b9\u4fbf\u7684 ",(0,r.jsx)(n.code,{children:"IEntitySeedData"})," \u4f9d\u8d56\u63a5\u53e3\u53ef\u4ee5\u5feb\u901f\u7684\u6784\u5efa\u79cd\u5b50\u6570\u636e\uff0c\u652f\u6301\u4efb\u4f55\u65e0\u53c2\u6784\u9020\u51fd\u6570\u5bf9\u8c61\u7c7b\u4e2d\u4f7f\u7528\u3002\u5982\u6211\u4eec\u9700\u8981\u4e3a ",(0,r.jsx)(n.code,{children:"Person"})," \u8868\u63d2\u5165\u521d\u59cb\u5316\u6570\u636e\uff1a"]}),"\n",(0,r.jsx)(n.h3,{id:"92221-\u5728\u5b9e\u4f53\u5b9a\u4e49\u4e2d\u4f7f\u7528",children:"9.22.2.1 \u5728\u5b9e\u4f53\u5b9a\u4e49\u4e2d\u4f7f\u7528"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {7,16-23}",children:'using Furion.DatabaseAccessor;\r\nusing System;\r\nusing System.Collections.Generic;\r\n\r\nnamespace Furion.Core\r\n{\r\n public class Person : EntityBase, IEntitySeedData\r\n {\r\n public string Name { get; set; }\r\n\r\n public int Age { get; set; }\r\n\r\n public string Address { get; set; }\r\n\r\n // \u914d\u7f6e\u79cd\u5b50\u6570\u636e\r\n public IEnumerable HasData(DbContext dbContext, Type dbContextLocator)\r\n {\r\n return new List\r\n {\r\n new Person { Id = 1, Name = "\u767e\u5c0f\u50e7", Address = "\u5e7f\u4e1c\u7701\u4e2d\u5c71\u5e02" },\r\n new Person { Id = 2, Name = "\u65b0\u751f\u5e1d", Address = "\u5e7f\u4e1c\u7701\u73e0\u6d77\u5e02" }\r\n };\r\n }\r\n }\r\n}\n'})}),"\n",(0,r.jsx)(n.admonition,{title:"\u7279\u522b\u6ce8\u610f",type:"important",children:(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"\u4e3b\u952e"})," \u503c\u5fc5\u987b\u624b\u52a8\u63d2\u5165\uff0c\u56e0\u4e3a\u4f1a\u81ea\u52a8\u5173\u95ed\u4e3b\u952e\u6216\u81ea\u589e\u6807\u8bc6\u68c0\u67e5\u3002"]})}),"\n",(0,r.jsx)(n.h3,{id:"92222-\u5728\u4efb\u610f\u5bf9\u8c61\u7c7b\u4e2d\u4f7f\u7528",children:"9.22.2.2 \u5728\u4efb\u610f\u5bf9\u8c61\u7c7b\u4e2d\u4f7f\u7528"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {6,9-16}",children:'using Furion.DatabaseAccessor;\r\nusing System.Collections.Generic;\r\n\r\nnamespace Furion.Application\r\n{\r\n public class PersonSeedData : IEntitySeedData\r\n {\r\n // \u914d\u7f6e\u79cd\u5b50\u6570\u636e\r\n public IEnumerable HasData(DbContext dbContext, Type dbContextLocator)\r\n {\r\n return new List\r\n {\r\n new Person { Id = 1, Name = "\u767e\u5c0f\u50e7", Address = "\u5e7f\u4e1c\u7701\u4e2d\u5c71\u5e02" },\r\n new Person { Id = 2, Name = "\u65b0\u751f\u5e1d", Address = "\u5e7f\u4e1c\u7701\u73e0\u6d77\u5e02" }\r\n };\r\n }\r\n }\r\n}\n'})}),"\n",(0,r.jsx)(n.h2,{id:"9223-\u5bfc\u822a\u5c5e\u6027",children:"9.22.3 \u5bfc\u822a\u5c5e\u6027"}),"\n",(0,r.jsxs)(n.p,{children:["\u901a\u5e38\u6211\u4eec\u7684\u5b9e\u4f53\u6709 ",(0,r.jsx)(n.code,{children:"\u4e00\u5bf9\u591a"}),"\uff0c",(0,r.jsx)(n.code,{children:"\u591a\u5bf9\u591a"}),"\u7b49\u5916\u952e\u5173\u7cfb\uff0c\u90a3\u4e48",(0,r.jsx)(n.strong,{children:"\u6211\u4eec\u9700\u8981\u5355\u72ec\u4e3a\u6bcf\u4e00\u4e2a\u5b9e\u4f53\u6dfb\u52a0\u6570\u636e\u79cd\u5b50\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5199\u5728\u4e3b\u8868\u4e2d\u3002"})]}),"\n",(0,r.jsx)(n.h2,{id:"9224-\u591a\u4e2a\u6570\u636e\u5e93\u79cd\u5b50\u6570\u636e",children:"9.22.4 \u591a\u4e2a\u6570\u636e\u5e93\u79cd\u5b50\u6570\u636e"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"Furion"})," \u63d0\u4f9b\u6cdb\u578b\u7684\u65b9\u5f0f\u652f\u6301\u591a\u4e2a\u6570\u636e\u5e93\u79cd\u5b50\u6570\u636e\u8bbe\u5b9a\uff0c\u5982\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {6,9-16}",children:'using Furion.DatabaseAccessor;\r\nusing System.Collections.Generic;\r\n\r\nnamespace Furion.Application\r\n{\r\n public class PersonSeedData : IEntitySeedData\r\n {\r\n // \u914d\u7f6e\u79cd\u5b50\u6570\u636e\r\n public IEnumerable HasData(DbContext dbContext, Type dbContextLocator)\r\n {\r\n return new List\r\n {\r\n new Person { Id = 1, Name = "\u767e\u5c0f\u50e7", Address = "\u5e7f\u4e1c\u7701\u4e2d\u5c71\u5e02" },\r\n new Person { Id = 2, Name = "\u65b0\u751f\u5e1d", Address = "\u5e7f\u4e1c\u7701\u73e0\u6d77\u5e02" }\r\n };\r\n }\r\n }\r\n}\n'})}),"\n",(0,r.jsxs)(n.p,{children:["\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\u540c\u65f6\u4e3a ",(0,r.jsx)(n.code,{children:"MySqlDbContext"})," \u548c ",(0,r.jsx)(n.code,{children:"SqliteDbContext"})," \u521b\u5efa\u79cd\u5b50\u6570\u636e\u3002"]}),"\n",(0,r.jsx)(n.h2,{id:"9225-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"9.22.5 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,r.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,r.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,r.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,r.jsx)(n.hr,{}),"\n",(0,r.jsx)(n.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,r.jsxs)(n.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,r.jsx)(n.code,{children:"\u6570\u636e\u79cd\u5b50"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,r.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/ef/core/modeling/data-seeding",children:"EF Core - \u6570\u636e\u79cd\u5b50\u8bbe\u5b9a"})," \u7ae0\u8282\u3002"]})})]})}function u(e={}){const{wrapper:n}={...(0,s.a)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(l,{...e})}):l(e)}},1151:(e,n,t)=>{t.d(n,{Z:()=>o,a:()=>d});var r=t(7294);const s={},i=r.createContext(s);function d(e){const n=r.useContext(i);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:d(e.components),r.createElement(i.Provider,{value:n},e.children)}}}]);