"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[8854],{3616:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>s,default:()=>a,frontMatter:()=>i,metadata:()=>c,toc:()=>d});var o=t(5893),r=t(1151);const i={id:"dbcontext-locator",title:"9.3 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",sidebar_label:"9.3 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668"},s=void 0,c={id:"dbcontext-locator",title:"9.3 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",description:"\u53ea\u8981\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u6ce8\u518c\u7ed1\u5b9a\u4e86\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\uff0c\u90a3\u4e48\u6240\u6709\u7684\u4ed3\u50a8\u3001\u5b9e\u4f53\u3001\u79cd\u5b50\u3001\u914d\u7f6e\u3001\u89c6\u56fe\u3001\u51fd\u6570\u7b49\u6570\u636e\u5e93\u76f8\u5173\u7684\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u90fd\u9700\u8981\u6307\u5b9a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\uff0c\u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u9664\u5916\u3002",source:"@site/docs/dbcontext-locator.mdx",sourceDirName:".",slug:"/dbcontext-locator",permalink:"/docs/dbcontext-locator",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/dbcontext-locator.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1682389320,formattedLastUpdatedAt:"Apr 25, 2023",frontMatter:{id:"dbcontext-locator",title:"9.3 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",sidebar_label:"9.3 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668"},sidebar:"docs",previous:{title:"9.2 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587",permalink:"/docs/dbcontext"},next:{title:"9.4 \u6570\u636e\u5e93\u5b9e\u4f53",permalink:"/docs/entity"}},l={},d=[{value:"9.3.1 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",id:"931-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",level:2},{value:"9.3.2 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u4f5c\u7528",id:"932-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u4f5c\u7528",level:2},{value:"9.3.3 \u5982\u4f55\u5b9a\u4e49\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",id:"933-\u5982\u4f55\u5b9a\u4e49\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",level:2},{value:"9.3.4 \u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",id:"934-\u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",level:2},{value:"9.3.5 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u652f\u6301\u5bf9\u8c61",id:"935-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u652f\u6301\u5bf9\u8c61",level:2},{value:"9.3.6 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"936-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function x(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(n.admonition,{title:"\u5c0f\u63d0\u9192",type:"important",children:[(0,o.jsxs)(n.p,{children:["\u53ea\u8981\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u6ce8\u518c\u7ed1\u5b9a\u4e86\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\uff0c\u90a3\u4e48\u6240\u6709\u7684",(0,o.jsx)(n.strong,{children:"\u4ed3\u50a8\u3001\u5b9e\u4f53\u3001\u79cd\u5b50\u3001\u914d\u7f6e\u3001\u89c6\u56fe\u3001\u51fd\u6570"}),"\u7b49\u6570\u636e\u5e93\u76f8\u5173\u7684",(0,o.jsx)(n.strong,{children:"\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5"}),"\u90fd\u9700\u8981\u6307\u5b9a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\uff0c\u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u9664\u5916\u3002"]}),(0,o.jsxs)(n.p,{children:["\u5982\u679c\u6539\u53d8\u4e86\u548c\u6570\u636e\u5e93\u5b9e\u4f53\u76f8\u5173\u7684\u6240\u6709\u914d\u7f6e\u63a5\u53e3\u7684\u5b9a\u4f4d\u5668\uff0c\u8fd8\u9700\u6267\u884c ",(0,o.jsx)(n.code,{children:"Add-Migration"})," \u548c ",(0,o.jsx)(n.code,{children:"Update-Database"})," \u547d\u4ee4\u3002"]})]}),"\n",(0,o.jsx)(n.h2,{id:"931-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",children:"9.3.1 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668"}),"\n",(0,o.jsxs)(n.p,{children:["\u5728\u4e86\u89e3\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u4e86\u89e3\u4ec0\u4e48\u662f ",(0,o.jsx)(n.code,{children:"\u5b9a\u4f4d\u5668"}),"\uff0c",(0,o.jsx)(n.code,{children:"\u5b9a\u4f4d\u5668"})," \u5c31\u662f\u7ed9\u7269\u4f53\u5b89\u88c5\u7279\u6b8a\u914d\u7f6e\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u5b9e\u65f6\u8ffd\u8e2a\u548c\u5b9a\u4f4d\u3002"]}),"\n",(0,o.jsxs)(n.p,{children:["\u90a3\u4e3a\u4ec0\u4e48\u9700\u8981 ",(0,o.jsx)(n.code,{children:"\u5b9a\u4f4d\u5668"}),"\uff1f"]}),"\n",(0,o.jsxs)(n.p,{children:["\u7531\u4e8e ",(0,o.jsx)(n.code,{children:"EF Core"})," \u672c\u8eab\u652f\u6301\u591a\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u64cd\u4f5c\uff0c\u4f46\u662f\u901a\u8fc7 ",(0,o.jsx)(n.code,{children:"\u4f9d\u8d56\u6ce8\u5165"})," \u7684\u65b9\u5f0f\u9ed8\u8ba4\u53ea\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\uff0c\u4e5f\u5c31\u662f\u5982\u679c\u6211\u4eec\u60f3\u8981\u64cd\u4f5c\u591a\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\uff0c\u90a3\u4e48 ",(0,o.jsx)(n.code,{children:"\u6784\u9020\u51fd\u6570"})," \u6ce8\u5165\u65b9\u5f0f\u5c31\u4f1a\u53d8\u5f97\u590d\u6742\u3002"]}),"\n",(0,o.jsxs)(n.p,{children:["\u6240\u4ee5\uff0c",(0,o.jsx)(n.code,{children:"Furion"})," \u5b9e\u73b0\u4e86\u4e00\u5957 ",(0,o.jsx)(n.code,{children:"\u5b9a\u4f4d\u5668"})," \u529f\u80fd\uff0c\u901a\u8fc7\u8fd9\u4e2a ",(0,o.jsx)(n.code,{children:"\u5b9a\u4f4d\u5668"})," \uff0c\u6211\u4eec\u5c31\u80fd\u591f\u901a\u8fc7 ",(0,o.jsx)(n.code,{children:"\u4f9d\u8d56\u6ce8\u5165"})," \u7b49\u591a\u4e2a\u65b9\u5f0f\u5b9a\u4f4d\u5230\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5e76\u521d\u59cb\u5316\u3002"]}),"\n",(0,o.jsx)(n.h2,{id:"932-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u4f5c\u7528",children:"9.3.2 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u4f5c\u7528"}),"\n",(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsx)(n.li,{children:"\u80fd\u591f\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u591a\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587"}),"\n",(0,o.jsxs)(n.li,{children:["\u80fd\u591f\u907f\u514d\u4e1a\u52a1\u5c42\u76f4\u63a5\u5f15\u7528 ",(0,o.jsx)(n.code,{children:"DbContext"})]}),"\n",(0,o.jsx)(n.li,{children:"\u80fd\u591f\u5b9e\u73b0\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u3001\u8bfb\u5199\u5206\u79bb\u3001\u4e3b\u4ece\u5e93\u7b49\u590d\u6742\u64cd\u4f5c"}),"\n"]}),"\n",(0,o.jsx)(n.h2,{id:"933-\u5982\u4f55\u5b9a\u4e49\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",children:"9.3.3 \u5982\u4f55\u5b9a\u4e49\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668"}),"\n",(0,o.jsx)(n.p,{children:"\u5b9a\u4e49\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u53ea\u9700\u9075\u5faa\u4e09\u4e2a\u539f\u5219\u5373\u53ef\uff1a"}),"\n",(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsxs)(n.li,{children:["\u5fc5\u987b\u662f\u516c\u5f00 ",(0,o.jsx)(n.code,{children:"class"})," \u7c7b\u578b\u540c\u65f6\u5177\u5907\u65e0\u53c2\u6784\u9020\u51fd\u6570"]}),"\n",(0,o.jsxs)(n.li,{children:["\u8be5\u7c7b\u578b\u5fc5\u987b\u7ee7\u627f ",(0,o.jsx)(n.code,{children:"IDbContextLocator"})," \u63a5\u53e3"]}),"\n",(0,o.jsx)(n.li,{children:"\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u548c\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5fc5\u987b\u662f\u4e00\u5bf9\u4e00\u5173\u7cfb\uff0c\u4e5f\u5c31\u662f\u4e0d\u80fd\u540c\u65f6\u88ab\u591a\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u4f7f\u7528"}),"\n"]}),"\n",(0,o.jsx)(n.p,{children:"\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u5b9a\u4e49\u4ee3\u7801\u5982\u4e0b\uff1a"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,5}",children:"using Furion.DatabaseAccessor;\r\n\r\nnamespace Furion.Core\r\n{\r\n public sealed class FurionDbContextLocator : IDbContextLocator\r\n {\r\n }\r\n}\n"})}),"\n",(0,o.jsx)(n.h2,{id:"934-\u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668",children:"9.3.4 \u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668"}),"\n",(0,o.jsxs)(n.p,{children:["\u5728 ",(0,o.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u5df2\u7ecf\u63d0\u4f9b\u4e86 ",(0,o.jsx)(n.code,{children:"MasterDbContextLocator"})," \u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\uff0c\u6240\u4ee5\u9ed8\u8ba4\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u53ea\u9700\u7ee7\u627f ",(0,o.jsx)(n.code,{children:"AppDbContext"})," \u5373\u53ef\u3002"]}),"\n",(0,o.jsx)(n.p,{children:"\u5982\uff1a\u53ea\u6709\u4e00\u4e2a\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4e49\uff1a"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'using Furion.DatabaseAccessor;\r\nusing Microsoft.EntityFrameworkCore;\r\n\r\nnamespace Furion.EntityFramework.Core\r\n{\r\n [AppDbContext("Sqlite3ConnectionString", DbProvider.Sqlite)]\r\n public class DefaultDbContext : AppDbContext // \u65e0\u9700\u6307\u5b9a\u5b9a\u4f4d\u5668\r\n {\r\n public DefaultDbContext(DbContextOptions options) : base(options)\r\n {\r\n }\r\n }\r\n}\n'})}),"\n",(0,o.jsxs)(n.admonition,{title:"\u5173\u4e8e\u591a\u6570\u636e\u5e93\u5b9a\u4f4d\u5668",type:"important",children:[(0,o.jsxs)(n.p,{children:["\u9ed8\u8ba4\u6570\u636e\u5e93\u7684\u5b9a\u4f4d\u5668\u9ed8\u8ba4\u4e3a ",(0,o.jsx)(n.code,{children:"MasterDbContextLocator"}),"\uff0c\u6240\u4ee5\u65e0\u9700\u663e\u793a\u6307\u5b9a\u5b9a\u4f4d\u5668\uff0c",(0,o.jsx)(n.strong,{children:"\u4f46\u4ece\u7b2c\u4e8c\u4e2a\u6570\u636e\u5e93\u5f00\u59cb\uff0c\u90fd\u5fc5\u987b\u6307\u5b9a\u6570\u636e\u5e93\u5b9a\u4f4d\u5668"}),"\u3002\u5982\uff1a"]}),(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsx)(n.li,{children:"\u6ce8\u518c\u4e0a\u4e0b\u6587\uff1a"}),"\n"]}),(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:"options.AddDbPool();\n"})}),(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsx)(n.li,{children:"\u5b9a\u4e49\u4e0a\u4e0b\u6587"}),"\n"]}),(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'using Furion.DatabaseAccessor;\r\nusing Microsoft.EntityFrameworkCore;\r\n\r\nnamespace Furion.EntityFramework.Core\r\n{\r\n [AppDbContext("Sqlite3ConnectionString", DbProvider.Sqlite)]\r\n public class OtherDbContext : AppDbContext // \u9700\u6307\u5b9a\u5b9a\u4f4d\u5668\r\n {\r\n public OtherDbContext(DbContextOptions options) : base(options)\r\n {\r\n }\r\n }\r\n}\n'})}),(0,o.jsxs)(n.p,{children:["\u53e6\u5916\uff0c",(0,o.jsx)(n.code,{children:"Entity/IEntity"})," \u548c ",(0,o.jsx)(n.code,{children:"IRepository"})," \u7b49\u90fd\u9700\u8981\u6307\u5b9a\u5b9a\u4f4d\u5668\uff0c\u5982\uff1a",(0,o.jsx)(n.code,{children:"IEntity"}),"\uff0c",(0,o.jsx)(n.code,{children:"IRepository"})," \u64cd\u4f5c\u3002"]})]}),"\n",(0,o.jsx)(n.h2,{id:"935-\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u652f\u6301\u5bf9\u8c61",children:"9.3.5 \u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5b9a\u4f4d\u5668\u652f\u6301\u5bf9\u8c61"}),"\n",(0,o.jsx)(n.p,{children:"\u76ee\u524d\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u652f\u6301\u4ee5\u4e0b\u591a\u4e2a\u5bf9\u8c61\uff1a"}),"\n",(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"AppDbContext"}),"\uff1a\u6570\u636e\u4e0a\u4e0b\u6587"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IRepository"}),"\uff1a\u5b9e\u4f53\u4ed3\u50a8"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"ISqlRepository"}),": Sql \u64cd\u4f5c\u4ed3\u50a8"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IDbRepository"}),": \u7279\u5b9a\u6570\u636e\u5e93\u64cd\u4f5c\u4ed3\u50a8"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IMSRepository"}),": \u8bfb\u5199\u5206\u79bb\u4ed3\u50a8"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"Func"}),"\uff1a\u4f9d\u8d56\u6ce8\u5165\u83b7\u53d6\u6570\u636e\u5e93\u4e0a\u4e0b\u6587"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"Entity"})," \uff1a\u5b9e\u4f53\u914d\u7f6e"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"EntityBase"}),"\uff1a\u5b9e\u4f53\u914d\u7f6e"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"EntityNotKey"}),"\uff1a\u65e0\u952e\u5b9e\u4f53\u914d\u7f6e"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IEntity"}),"\uff1a\u9ed8\u8ba4\u5b9e\u4f53\u914d\u7f6e"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IEntitySeedData"}),"\uff1a\u79cd\u5b50\u6570\u636e\u914d\u7f6e"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IEntityTypeBuilder"}),"\uff1a\u5b9e\u4f53\u7c7b\u578b\u6784\u5efa\u5668"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"IModelBuilderFilter"}),"\uff1a\u6a21\u578b\u6784\u5efa\u7b5b\u9009\u5668"]}),"\n",(0,o.jsxs)(n.li,{children:[(0,o.jsx)(n.code,{children:"[QueryableFunction(DbContextLocators=Type[])]"}),"\uff1a\u67e5\u8be2\u51fd\u6570"]}),"\n"]}),"\n",(0,o.jsx)(n.h2,{id:"936-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"9.3.6 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,o.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,o.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,o.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})})]})}function a(e={}){const{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(x,{...e})}):x(e)}},1151:(e,n,t)=>{t.d(n,{Z:()=>c,a:()=>s});var o=t(7294);const r={},i=o.createContext(r);function s(e){const n=o.useContext(i);return o.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),o.createElement(i.Provider,{value:n},e.children)}}}]);