{"id":39,"date":"2019-04-14T21:10:51","date_gmt":"2019-04-14T13:10:51","guid":{"rendered":"http:\/\/ones.software\/blog\/?p=39"},"modified":"2019-04-14T21:10:51","modified_gmt":"2019-04-14T13:10:51","slug":"saas-002","status":"publish","type":"post","link":"https:\/\/ones.software\/code\/zh\/saas-002\/","title":{"rendered":"SaaS\u7684\u591a\u79df\u6236\u6a21\u5f0f(Multi-tenancy)\u5be6\u8e10\uff1a.NET Core + Entity Framework Core"},"content":{"rendered":"\n\t\t\t\t\n<h4>\u4f01\u696d\u7d1a\u8edf\u4ef6\u8a02\u95b2\u670d\u52d9 (SaaS) \u7684\u958b\u767c&nbsp;(2)<\/h4>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Uwhb6v3rAvJe93pPe24ZIg.png\" alt=\"\"\/><figcaption><strong>\u591a\u79df\u6236\u6a21\u5f0f (Multi&nbsp;tenancy)<\/strong><\/figcaption><\/figure>\n\n\n\n<p>\u6211\u5011\u65bc<a href=\"https:\/\/ones.software\/blog\/index.php\/2019\/04\/11\/saas-001\/\">\u4e0a\u4e00\u7bc7<\/a>\u63d0\u904e\u95dc\u65bc<strong>\u591a\u79df\u6236\u6a21\u5f0f (Multi tenancy) <\/strong>\u7684\u5404\u7a2e\u597d\u8655\uff0c\u4eca\u65e5\u6211\u5011\u5c31\u4f86\u8ac7\u8ac7\u5982\u4f55\u53bb\u5be6\u969b\u67b6\u69cb\u51fa\u4e00\u500b\u591a\u7528\u6236\u6a21\u5f0f\u7684<strong>\u670d\u52d9\u5668\u67b6\u69cb\uff0c<\/strong>\u4e26\u80fd\u5be6\u73fe<strong>\u5ba2\u6236\u7368\u7acb<\/strong>\u3001<strong>\u7a69\u5065\u3001\u5bb9\u6613\u7dad\u8b77<\/strong>\u7684\u7279\u9ede\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u591a\u7528\u6236\u6a21\u5f0f\u7684\u5be6\u73fe\u65b9\u6cd5\u53ef\u4ee5\u5206\u7232\u5e7e\u7a2e:<\/p>\n\n\n\n<ol><li>\u670d\u52d9\u5668\u4f7f\u7528<strong>\u540c\u4e00\u500b\u57df\u540d (Domain Name)<\/strong>\uff0c\u5728\u6bcf\u4e00\u500b<strong>\u8acb\u6c42 (Request)<\/strong> \u88cf\u9762\u90fd\u52a0\u4e0a\u4e00\u500b <strong>TenantId<\/strong> \u7684\u53c3\u6578\u4f5c\u5206\u8fa8\u3002<\/li><li>\u670d\u52d9\u5668<strong>\u4f7f\u7528\u540c\u4e00\u500b\u57df\u540d<\/strong>\uff0c\u7136\u5f8c\u4ee5\u4e0d\u540c\u7684\u7528\u6236ID \u4f86\u4f5c\u5340\u5206\u3002<\/li><li>\u670d\u52d9\u5668\u63a5\u53d7<strong>\u4e0d\u540c\u57df\u540d<\/strong>\uff0c\u6bcf\u500b\u5ba2\u6236\u90fd\u7d66\u4e88\u4ed6\u4e00\u500b<strong>\u5c08\u5c6c\u57df\u540d<\/strong>\uff0c\u4e26<strong>\u4ee5\u57df\u540d\u53bb\u5340\u5206<\/strong>\u3002<\/li><\/ol>\n\n\n\n<p>\u9019\u88cf\u6703\u793a\u7bc4\u5be6\u73fe<strong>\u7b2c\u4e09\u500b<\/strong>\u65b9\u6848\uff0c\u5176\u512a\u9ede\u6709\uff1a<\/p>\n\n\n\n<ul><li>\u53ef\u4ee5\u7528\u4e0d\u540c<strong>\u5b50\u57df\u540d (Sub-domain) <\/strong>\u53bb\u5206\u7d66\u4e0d\u540c\u7528\u6236\uff0c\u4f8b\u5982 tenantA.example.com\uff0ctenantB.example.com\u3002\u5982\u679c\u8acb\u6c42\u88cf\u9762\u52a0\u6709\u7528\u6236\u7684\u540d\u5b57\u7684\u8a71\uff0c\u80fd\u5920\u8b93\u7528\u6236\u611f\u89ba\u65b9\u6848\u662f\u4ed6\u5011\u5c08\u6709\u7684\u670d\u52d9\u3002<\/li><li>\u5982\u679c\u5ba2\u6236\u81ea\u5df1\u516c\u53f8\u6709\u60f3\u7528\u7684\u57df\u540d\u7684\u8a71\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u3002\u53ea\u8981\u5728\u4ed6\u5011\u7684<strong>\u57df\u540d\u670d\u52d9\u5668 (Domain Name Server)<\/strong>\u4e0a\uff0c\u628a\u57df\u540d\u6307\u5230\u6211\u5011\u670d\u52d9\u5668\u7684\u5730\u5740\u4fbf\u53ef\u3002<\/li><li>\u5c0d\u670d\u52d9\u5668\u4f86\u8aac\uff0c\u53ea\u8981\u5c0f\u5fc3\u8655\u7406\uff0c\u7528\u54ea\u500b\u65b9\u6848\u5dee\u5225\u90fd\u4e0d\u5927\uff0c\u4f46\u662f\u5728<strong>\u700f\u89bd\u5668 (Browser)<\/strong>\u4e0a\u5dee\u5225\u5c31\u5f88\u5927\u3002\u65b9\u6848 1 \u548c 2 \u90fd\u662f\u4f7f\u7528\u540c\u4e00\u500b\u57df\u540d\u9032\u5165\uff0c\u5373\u4f7f\u6240\u6709\u5ba2\u6236\u4e0d\u540c\uff0c\u4ed6\u5011\u5728\u700f\u89bd\u5668\u4e0a\u7684 Cookie \u6216\u8005 Local Storage \u7684\u7a7a\u9593\u90fd\u662f\u5171\u901a\u3002\u96d6\u7136\u8aac\u5728\u5be6\u969b\u74b0\u5883\u4e2d\uff0c\u540c\u4e00\u81fa\u96fb\u8166\u767b\u5165\u4e0d\u540c\u5ba2\u6236\u7684\u60c5\u6cc1\u975e\u5e38\u7f55\u898b\u3002\u4f46\u4e00\u65e6\u767b\u5165\u4e0d\u540c\u5ba2\u6236\uff0c\u5c31\u5f88\u5bb9\u6613\u767c\u751f\u8cc7\u6599\u6709\u885d\u7a81\u3002\u9019\u6a23\u7684\u8a71\uff0c\u5c31\u4e0d\u5bb9\u6613\u9054\u5230<strong>\u5ba2\u6236\u7368\u7acb<\/strong>\u9019\u500b\u689d\u4ef6\u3002\u76f8\u53cd\u65b9\u6848 3\u7684\u8a71\uff0c\u6bcf\u4e00\u500b\u5ba2\u6236\u90fd\u4f7f\u7528\u4e0d\u540c\u7684\u57df\u540d\uff0c\u5728\u700f\u89bd\u5668\u4e0a\u7684\u7684\u5b58\u53d6\u7a7a\u9593\u4e5f\u6703\u662f\u7368\u7acb\u7684\uff0c\u5c31\u4e0d\u6703\u767c\u751f\u4ee5\u4e0a\u7684\u554f\u984c\u3002<\/li><\/ul>\n\n\n\n<p>\u9592\u8a71\u4f11\u63d0\uff0c\u8981\u958b\u59cb\u5be6\u969b\u793a\u7bc4\u4e86\u3002\u9019\u500b\u793a\u7bc4\u67b6\u69cb\u7684\u6982\u5ff5\u662f\u300c<strong>\u9760\u9802\u7aef\u5224\u65b7\u79df\u6236\u8cc7\u6599\uff0c\u518d\u65bc\u672b\u7aef\u53bb\u9032\u884c\u5206\u985e\u8655\u7406<\/strong>\u300d\u3002\u9019\u88cf\u6211\u5011\u6703\u5229\u7528&nbsp;<strong>.Net Core<\/strong> +<strong> Entity Framework Core <\/strong>\u4f86\u5be6\u73fe\u4e00\u500b\u591a\u79df\u6236\u6a21\u5f0f\u7684\u4f8b\u5b50\u3002\u9019\u500b\u4f8b\u5b50\u8981\u7b26\u5408\u4e09\u500b\u8981\u6c42\uff1a<\/p>\n\n\n\n<ol><li>\u6839\u64da\u4e0d\u540c<strong>\u8acb\u6c42\u57df\u540d (Request Host Name)\uff0c<\/strong>\u8fd4\u56de\u5c0d\u61c9\u7684\u5ba2\u6236\u8cc7\u6599\uff0c\u5982\u540d\u5b57\uff1b<\/li><li>\u6839\u64da\u4e0d\u540c\u57df\u540d\uff0c\u5b58\u53d6<strong>\u4e0d\u540c\u7684\u6578\u64da\u5eab (Database)<\/strong>\uff1b<\/li><li>\u6839\u64da\u4e0d\u540c\u57df\u540d\uff0c<strong>\u4e0a\u8f09\u548c\u4e0b\u8f09\u6a94\u6848(File) <\/strong>\u9032\u53bb<strong>\u4e0d\u540c\u7684\u6587\u4ef6\u593e (Folder)<\/strong>\u3002<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>\u5be6\u969b\u64cd\u4f5c<\/h3>\n\n\n\n<p>1.\u9996\u5148\u6253\u958b Visual Studio\uff0c\u65b0\u589e\u4e00\u500b&nbsp;.Net Core \u7684 Project\uff0c\u9019\u88cf\u6211\u5011\u7528 2.2\u7248\u672c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*6ZQCvjKclTEFTZVa-iOE0A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>2.\u5b9a\u7fa9 <strong>Tenant (\u79df\u5ba2)<\/strong>\uff0c\u88cf\u9762\u5b58\u653e\u4e86\u8a72\u5ba2\u6236\u7684\u8cc7\u6599\uff0c\u5982<strong>\u5ba2\u6236\u540d\u5b57<\/strong>\uff0c<strong>\u6578\u64da\u5eab\u7684\u5730\u5740 (ConnectionString)<\/strong>\uff0c\u5b58\u53d6\u6a94\u6848\u7684<strong>\u6587\u4ef6\u593e\uff0c<\/strong>\u548c\u9019\u500b\u5ba2\u6236<strong>\u63a5\u53d7\u7684\u6240\u6709\u57df\u540d\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*EDK0pPU_LQ0rMFeBvh1iGA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>3.\u5b9a\u7fa9<strong>TenantContext<\/strong>\uff0c\u7528\u4f86\u7de9\u5b58\u7576\u524d\u7684Scope\u73fe\u5728\u662f\u54ea\u4e00\u500b\u79df\u6236\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*bRiDDszEAMtfZP1fkoRM-Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>4.\u5b9a\u7fa9<strong>TenantResolver<\/strong>\uff0c\u4f86\u6839\u64da\u4e0d\u540c\u7684\u57df\u540d\u53bb\u641c\u5c0b\u81ea\u5df1\u5c6c\u65bc\u54ea\u4e00\u500b\u79df\u6236\u3002\u9019\u88cf\u7232\u4e86\u65b9\u4fbf\u793a\u7bc4\uff0c\u6240\u6709\u76f4\u63a5\u52a0\u5165\u4e86\u5169\u500b\u5ba2\u6236\u7684\u8cc7\u6599\u5728\u9019\u88cf\uff0c\u4f46\u6b63\u5f0f\u61c9\u8a72\u8981\u8b80\u53d6\u4e00\u500b\u984d\u5916\u5132\u5b58\u6240\u6709\u79df\u6236\u8cc7\u6599\u7684\u6a94\u6848\uff0c\u6216\u8005\u662f\u9023\u63a5\u5230\u4e00\u500b\u9060\u7aef\u7684\u79df\u6236\u7ba1\u7406\u670d\u52d9\u5668\uff0c\u4f86\u8b80\u53d6\u79df\u6236\u8cc7\u6599\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*28HXEG5GWQRASDsmFXmOxQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u9019\u88cf\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u662f\u7d93\u904e localhost:5000 \u9032\u4f86\u7684\u5c31\u662f\u5ba2\u6236A\uff0clocalhost:5001\u9032\u4f86\u7684\u5c31\u662f\u5ba2\u6236B\u3002<\/p>\n\n\n\n<p>5. \u5230\u9019\u88cf\u7232\u6b62\uff0c\u5c31\u6709\u4e86\u6700\u57fa\u672c\u7684\u69cb\u6210\u4e86\u3002\u7136\u5f8c\u5c31\u662f<strong>\u5982\u4f55\u53bb\u5224\u65b7\u8acb\u6c42\u662f\u5c6c\u65bc\u54ea\u4e00\u500b\u79df\u6236<\/strong>\uff0c\u9019\u88cf\u4f7f\u7528<strong>\u4e2d\u9593\u4ef6 (Middleware)<\/strong>\uff0c\u4f86\u5b9a\u7fa9\u4e00\u500bHttpTenantResolveMiddleware \u53bb\u5224\u65b7\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*5zJ_E8X3bL0sy_xFUIaPDg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5728 HttpContext \u88cf\u9762\u80fd\u5920\u627e\u56de\u7576\u524d Request \u7684 HostName\uff0c\u4e26\u901a\u904eTenantResolver \u88cf\u662f\u5c6c\u65bc\u54ea\u4e00\u500b Tenant\uff0c\u6700\u5f8c\u653e\u9032\u53bb TenantContext \u88cf\u9762\u4fbf\u53ef\u3002<\/p>\n\n\n\n<p>6.\u5728 <code>startup.cs<\/code>\uff0c\u628a\u4e4b\u524d\u5b9a\u7fa9\u7684 Class \u90fd\u52a0\u9032\u53bb\u4f9d\u8cf4\u6ce8\u5165\uff0cTenantResolver \u7528 Singleton\uff0c\u5176\u4ed6\u7684\u7528 Scope\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*KdLpWQtpXXNyCRJBmCY2Gw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u9019\u88cf\u6709\u4e00\u500b\u5de7\u5999\u7684\u5730\u65b9\uff0c\u5c31\u662f\u628a TenantContext \u88cf\u9762\u7684 Tenant \u518d\u653e\u9032Scope\u88cf\u9762\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u66f4\u76f4\u63a5\u7684\u5728\u62ff\u5230\u7576\u524d Scope \u7684\u79df\u6236\u3002<\/p>\n\n\n\n<p>7.\u52a0\u5165 HttpTenantResolveMiddleware \u9032<strong>\u6d41\u6c34\u7dab (Pipeline)<\/strong>\u3002\u5224\u65b7 Tenant\u9019\u500b\u6d41\u7a0b\uff0c\u8d8a\u65e9\u968e\u6bb5\u505a\u8d8a\u597d\uff0c\u6240\u4ee5\u9019\u88cf\u76f4\u63a5\u653e\u5230\u7b2c\u4e00\u4f4d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*lXvUb2oLMprf3093rslBNw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>8. \u9019\u694a\u5c31\u53ef\u4ee5\u958b\u59cb\u7c21\u55ae\u7684\u6e2c\u8a66\u4e00\u4e0b\u3002\u9996\u5148\u5728 launchSetting \u88cf\u9762\u52a0\u4e0a\u5169\u500bTenant \u7684 Url\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*g-boiSbHXSJBAoOAU7e1XA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5b9a\u7fa9\u4e00\u500b\u6e2c\u8a66\u7684 Controller\uff0c\u88cf\u9762\u76f4\u63a5 <strong>\u6ce8\u5165 (inject)<\/strong> Tenant \u9032\u53bb\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*3U-fram5b7U8LNmI8iYMog.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u7136\u5f8c\u5c31\u53ef\u4ee5\u770b\u5230\u6e2c\u8a66\u7d50\u679c\u3002\u540c\u4e00\u689d\u8def\u5f91\uff0c\u540c\u4e00\u500b\u670d\u52d9\u5668\uff0c\u4f46\u7d93\u904e\u4e0d\u540c\u57df\u540d\u7684\u8acb\u6c42\uff0c\u8fd4\u56de\u4e0d\u540c\u79df\u6236\u7684\u8cc7\u6599\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*ikG1LyHxjJD0t1OJMunG7g.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5230\u9019\u88cf\uff0c\u57fa\u672c\u7684\u79df\u6236\u5206\u985e\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>\u6578\u64da\u5eab\u5b58\u53d6 \u200a\u2014\u200aEntity Framework Core<\/h3>\n\n\n\n<p>\u63a5\u8457\u4f86\u793a\u7bc4\u4e00\u4e9b\u5c0d Db \u7684\u5be6\u969b\u64cd\u4f5c\uff0c\u9019\u88cf\u7528 SqlServer \u4f86\u505a\u793a\u7bc4\u3002<\/p>\n\n\n\n<p>1.\u8981\u52a0\u5165\u4e09\u500b Entity Framework Core \u9700\u8981\u7684 Nuget Project\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/1200\/1*v8ovf8F1ZNjRxInZTnPzpQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>2.\u6211\u5011\u5c31\u5b9a\u7fa9\u4e00\u500b\u7c21\u55ae\u7684 TenantDbContext \u7684\u6578\u64da\u5eab\uff0c\u88cf\u9762\u6709\u4e00\u500b Key value pair \u7684\u8868\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/1200\/1*r7F4RXFXvLt3k6ZrYSB5Hg.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*gRYBaNhd8bRKnU2FDfBOXg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>3.\u5728 <code>startup.cs<\/code> \u88cf\u9762\u6ce8\u5165 TenantDbContext\u3002\u5e73\u6642\u7684 Entity Framework \u6559\u5b78\u88cf\uff0c\u90fd\u6703\u5728\u9019\u88cf\u52a0\u5165<strong>\u6578\u64da\u5eab\u7684\u5730\u5740 (connectionString) <\/strong>\u52a0\u9032\u53bb\uff0c\u4f46\u6211\u5011\u5c31\u4e0d\u5728\u9019\u88cf\u52a0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*90FSaBZj5wDvaLzGHyJCrw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>4. \u518d\u4f86\u4e00\u500b\u5c0f\u7ac5\u9580\uff0c\u5728 TenantDbContext \u4e5f\u6ce8\u5165 Teannt\u3002\u8986\u84cb\u6389\u539f\u4f86\u7684 OnConfiguring\uff0c\u5728\u9019\u88cf\u7528 Tenant \u88cf\u9762\u7684 ConnectionString \u53bb\u6307\u5b9a\u6578\u64da\u5eab\u8def\u5f91\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*M6GhRkrdkiQcFUy99PpIdg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>5.\u7136\u800c\uff0c\u5982\u679c\u73fe\u5728\u76f4\u63a5\u904b\u884c add-migration \u53bb\u6dfb\u52a0 MIgration \u7684\u8a71\uff0c\u5c31\u6703\u767c\u751f\u932f\u8aa4\u3002\u56e0\u7232\u5728\u6c92\u6709\u8acb\u6c42\u7684\u6642\u5019\uff0c\u662f\u4e0d\u6703\u77e5\u9053\u73fe\u5728\u662f\u5c6c\u65bc\u54ea\u500b\u79df\u6236\uff0c\u548c\u61c9\u8a72\u9023\u53bb\u54ea\u500b DB\u3002<\/p>\n\n\n\n<p>\u4f46\u5176\u5be6\u9019\u500b\u554f\u984c\u5f88\u5bb9\u6613\u89e3\u6c7a\u3002\u53ea\u8981\u52a0\u4e00\u500b Class \u53bbImplement IDesignTimeDbContextFactory&lt;TenantDbContext&gt; \u5c31\u53ef\u4ee5\u3002\u5728\u904b\u884c add-migration\u7684\u6642\u5019\uff0c\u5c31\u6703\u6839\u64da\u9019\u88cf\u53bb\u5275\u5efa TenantDbContext\uff0c\u53ea\u8981\u5728\u9019\u88cf\u7d66\u4e00\u500b\u5047\u7684\u4f4f\u6236\u8cc7\u6599\u9032\u53bb\uff0cadd-migraiton \u7684\u6642\u5019\u5c31\u53ef\u4ee5\u9760\u9019\u500bTenant\u7684\u6578\u64da\u5eab\u8def\u5f91\uff0c\u53bb\u8a08\u7b97 Migration\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*6bGKi5CJfZii_FAzlcxU2g.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4>Migration \u6210\u529f\u505a\u51fa\u4f86\u4e86\uff01<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*tbjhb2TVHdsK8YUF7IqO4w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>6.\u7136\u5f8c\u5c31\u5230\u5efa\u7acb\u7684\u6578\u64da\u5eab\u7684\u6642\u5019\u3002\u5728<code>startup.cs<\/code> configure \u7684\u6700\u5f8c\u968e\u6bb5\uff0c\u5c0d\u6bcf\u4e00\u500b Tenant \u90fd\u5275\u9020\u4e00\u500b\u65b0\u7684 Scope\uff0c\u5c07\u8a72 Tenant \u653e\u9032 TenantContext\uff0c\u518d\u904b\u884c Migration \u5c31\u53ef\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*vZQH5y3kT-KlMTcwSrNYMQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u904b\u884c\u5b8c\u5f8c\u770b\u4e00\u770b SSMS\uff0c\u5169\u500b\u5ba2\u6236\u7684\u6578\u64da\u5eab\u90fd\u61c9\u80fd\u6210\u529f\u5275\u9020\u51fa\u4f86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*KvE3NGPNAJ_90vdrYry5Zg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>7.\u958b\u59cb\u5be6\u969b\u6e2c\u8a66\uff0c\u505a\u4e00\u500b\u6e2c\u8a66\u6578\u64da\u5eab\u7684 Controller\u3002\u5982\u679c\u719f\u6089 Entity Framework\u7684\u4eba\u5c31\u6703\u767c\u73fe\uff0c\u5728\u9019\u88cf\u5c0d Entity Framework \u7684\u7528\u6cd5\u548c\u5e73\u6642\u7684\u7528\u6cd5<strong>\u6c92\u6709\u4efb\u4f55\u5206\u5225<\/strong>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*6WCoq94Lj3hJ5vNx56bMnw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6e2c\u8a66\u958b\u3002\u9810\u5148\u5c07\u8a2d\u4e86\u5ba2\u6236A\u7684 <strong>hello<\/strong>=<strong>I\u2019m Tenant A<\/strong>, \u7136\u5f8c\u5c31\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u7684\u57df\u540d\u62ff\u5230\u7684\u7d50\u679c\u662f\u4e0d\u4e00\u6a23\uff0c\u5169\u500b\u6578\u64da\u5eab\u662f<strong>\u5b8c\u5168\u5206\u958b<\/strong>\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*bII8kmZJPsgs3fyMrgHMww.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3>\u6a94\u6848\u5b58\u53d6<\/h3>\n\n\n\n<p>1.\u9019\u500b\u505a\u6cd5\u4e5f\u5f88\u7c21\u55ae\uff0c\u505a\u4e00\u500b TenantFileService\uff0c\u4e5f\u662f\u4e00\u958b\u59cb\u5c31\u6ce8\u5165Tenant\u3002\u9019\u88e1\u90fd\u7528 Tenant \u88cf\u9762\u7684 FileDirectory \u4f5c\u7232\u8def\u5f91\u7684\u57fa\u790e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*97eK5nIagsVPUt2Rh9Kh3w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>2. \u5728<code>startup.cs<\/code>\u7684\u52a0\u5230 Dependency Injection \u88cf\u9762\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*AbuFjj7Sn6uW6qOWrH3_7w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>3.\u5be6\u969b\u6e2c\u8a66\uff0c\u548c\u6578\u64da\u5eab\u7684\u6e2c\u8a66\u4e00\u6a23\uff0c\u4e5f\u662f Controller \u6ce8\u5165TenantFileService\uff0c\u76f4\u63a5\u4f7f\u7528\u4fbf\u53ef\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*oGUIx_HDrrpSqRfx1BOnKw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5927\u5bb6\u53ef\u4ee5\u770b\u5230\u7d93\u904e\u4e0d\u540c\u7684\u57df\u540d\u4e0a\u8f09\u7684\u6a94\u6848\uff0c\u6703\u5206\u5225\u653e\u9032\u4e0d\u540c\u7684\u6587\u4ef6\u593e\u88cf\u9762\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O56N0APkmBKU4uFDWOlHpQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>\u7e3d\u7d50<\/h3>\n\n\n\n<p>\u5728<strong><a href=\"https:\/\/ones.software\/blog\/index.php\/2019\/04\/11\/saas-001\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"\u4e0a\u4e00\u7bc7 (opens in a new tab)\">\u4e0a\u4e00\u7bc7<\/a><\/strong>\u88cf\u9762\u63d0\u5230\u904e,\u5118\u7ba1\u662f\u5728<strong>\u591a\u79df\u6236\u6a21\u5f0f(Multi Tenancy)<\/strong>\u7684\u74b0\u5883\u4e0b\uff0c \u7528\u6236\u8cc7\u6599\u7684<strong>\u5b89\u5168\u548c\u4fdd\u5bc6<\/strong>\u9084\u662f\u6700\u91cd\u8981\u7684\u8003\u616e\u3002\u800c\u9019\u500b\u4f8b\u5b50\u7684\u793a\u7bc4\u7684\u7cfb\u7d71\u67b6\u69cb\uff0c\u5c31\u6b63\u6b63\u662f\u7232\u4e86\u9054\u5230\u9019\u500b\u76ee\u7684\u3002<\/p>\n\n\n\n<p>\u88cf\u9762\u63d0\u5230\u904e,\u5118\u7ba1\u662f\u5728<strong>\u591a\u79df\u6236\u6a21\u5f0f(Multi Tenancy)<\/strong>\u7684\u74b0\u5883\u4e0b\uff0c \u7528\u6236\u8cc7\u6599\u7684<strong>\u5b89\u5168\u548c\u4fdd\u5bc6<\/strong>\u9084\u662f\u6700\u91cd\u8981\u7684\u8003\u616e\u3002\u800c\u9019\u500b\u4f8b\u5b50\u7684\u793a\u7bc4\u7684\u7cfb\u7d71\u67b6\u69cb\uff0c\u5c31\u6b63\u6b63\u662f\u7232\u4e86\u9054\u5230\u9019\u500b\u76ee\u7684\u3002<\/p>\n\n\n\n<p>\u7576\u4e00\u500b\u7cfb\u7d71\u4e0d\u65b7\u958b\u767c\u671f\u9593\uff0c\u958b\u767c\u8005\u4e0d\u53ef\u80fd\u5beb<strong>\u6bcf\u4e00\u500b\u529f\u80fd<\/strong>\u7684\u6642\u5019\uff0c\u90fd\u8981\u9867\u616e\u5230\u5982\u4f55\u53bb\u5340\u5206\u73fe\u5728\u662f\u5c6c\u65bc\u54ea\u500b\u4e00\u79df\u6236\uff0c\u56e0\u7232\u9019\u6a23\u5f88\u5bb9\u6613\u6703\u767c\u751f\u4eba\u7232\u932f\u8aa4\uff0c\u4e5f\u6703\u4ee4\u5230\u4ee3\u78bc\u6df7\u4e82\uff0c\u96e3\u4ee5\u7dad\u8b77\u3002<\/p>\n\n\n\n<p>\u800c\u9019\u500b\u7d50\u69cb\u7684\u597d\u8655\u662f\uff0c\u5728<strong>\u6700\u9802\u7aef\u7684\u968e\u6bb5<\/strong>\u5df2\u7d93\u78ba\u7acb\u597d\u9019\u500b\u8acb\u6c42\u662f\u5c6c\u65bc\u90a3\u500b\u4e00\u79df\u6236\uff0c\u7136\u5f8c\u5230\u4e86<strong>\u6700\u672b\u7aef\u7684 IO <\/strong>\u7684\u90e8\u5206\uff0c\u6839\u64da\u6700\u9802\u7aef\u7684\u5224\u65b7\u597d\u7684\u79df\u6236\u8cc7\u6599\u53bb\u6c7a\u5b9a\u5982\u4f55\u5b58\u53d6\uff0c\u800c<strong>\u4e2d\u9593\u7684\u908f\u8f2f\u90e8\u5206\u5c31\u5b8c\u5168\u4e0d\u7528\u9867\u616e\u5230\u79df\u6236\u7684\u554f\u984c<\/strong>\uff0c\u5c31\u7576\u666e\u901a\u7684\u670d\u52d9\u5668\u53bb\u958b\u767c\u529f\u80fd\u4fbf\u53ef\u3002<\/p>\n\n\n\n<p>\u7576\u7136\u5be6\u969b\u7684<strong>\u591a\u79df\u6236\u6a21\u5f0f<\/strong>\u7684\u4e0d\u6b62\u9019\u9ebd\u7c21\u55ae\uff0c\u9084\u6709\u5f88\u591a\u7bc4\u570d\u8981\u9867\u53ca\uff0c\u5982<strong>\u80cc\u666f\u5de5\u4f5c (Background Job)<\/strong>\uff0c<strong>\u7de9\u5b58 (Cache)<\/strong>\uff0c <strong>WebSocket<\/strong>\uff0c <strong>\u65e5\u8a8c\u6587\u4ef6 (Logging) <\/strong>\u7b49\u7b49\u3002\u4f46\u53ea\u8981\u7262\u8a18\u9019\u9019\u500b\u4f8b\u5b50\u7684\u6982\u5ff5 \u300c<strong>\u9760\u9802\u7aef\u5224\u65b7\u79df\u6236\u8cc7\u6599\uff0c\u518d\u65bc\u672b\u7aef\u53bb\u9032\u884c\u5206\u985e\u8655\u7406\u300d<\/strong>\uff0c\u90a3\u7121\u8ad6\u529f\u80fd\u5982\u4f55\u589e\u52a0\u64f4\u5927\uff0c\u4e5f\u80fd\u505a\u597d\u7dad\u8b77\u5de5\u4f5c\uff0c\u800c\u4e0d\u6703\u767c\u751f\u7528\u6236\u8cc7\u6599\u6df7\u4e82\u7684\u4e8b\u60c5\u767c\u751f\u3002<\/p>\n\n\n\n<p>\u5927\u5bb6\u53ef\u4ee5\u4e0a\u6211\u5011\u7684Github\uff0c\u53bb\u4e0b\u8f09\u9019\u500b project \u5be6\u969b\u4f86\u8a66\u8a66\u770b\u3002 <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/ones-software\/dotnet-core-multi-tenancy-sample\" target=\"_blank\">https:\/\/github.com\/ones-software\/dotnet-core-multi-tenancy-sample<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>ONEs software:  <a href=\"https:\/\/ones.software\/hk\/bookings-one\/\">https:\/\/ones.software\/hk\/bookings-one\/<\/a><\/p>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\t\t\t\t\u6211\u5011\u65bc\u4e0a\u4e00\u7bc7\u63d0\u904e\u95dc\u65bc\u591a\u79df\u6236\u6a21\u5f0f (Multi tenancy) \u7684\u5404\u7a2e\u597d\u8655\uff0c\u4eca\u65e5\u6211\u5011\u5c31\u4f86\u8ac7\u8ac7\u5982\u4f55\u53bb\u5be6\u969b\u67b6\u69cb\u51fa\u4e00\u500b\u591a\u7528\u6236\u6a21\u5f0f\u7684\u670d\u52d9\u5668\u67b6\u69cb\uff0c\u4e26\u80fd\u5be6\u73fe\u5ba2\u6236\u7368\u7acb\u3001\u7a69\u5065\u3001\u5bb9\u6613\u7dad\u8b77\u7684\u7279\u9ede\u3002\t\t<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[12],"tags":[18,35,67,80,101,126],"_links":{"self":[{"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/posts\/39"}],"collection":[{"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ones.software\/code\/zh\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}