{"id":194015,"date":"2024-11-13T03:03:18","date_gmt":"2024-11-12T19:03:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194015\/"},"modified":"2024-11-13T03:03:20","modified_gmt":"2024-11-12T19:03:20","slug":"%e4%b8%80%e5%80%8b%e6%b5%81%e8%a1%8c%e7%9a%84%e6%94%af%e6%8c%81%e8%b6%85%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84orm%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194015\/","title":{"rendered":"\u4e00\u500b\u6d41\u884c\u7684\u652f\u6301\u8d85\u591a\u6578\u64da\u5eab\u7684ORM\u5eab"},"content":{"rendered":"<h1 id=\"%e4%b8%80%e5%80%8b%e6%b5%81%e8%a1%8c%e7%9a%84%e6%94%af%e6%8c%81%e8%b6%85%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84orm%e5%ba%ab-RjuqTiQXXB\">\u4e00\u500b\u6d41\u884c\u7684\u652f\u6301\u8d85\u591a\u6578\u64da\u5eab\u7684ORM\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u5c0d\u65bc\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u5df2\u7d93\u6210\u70ba\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u958b\u767c\u8005\u9700\u8981\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u9019\u6642\uff0c\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff08ORM\uff09\u5eab\u4fbf\u61c9\u904b\u800c\u751f\u3002ORM\u5eab\u80fd\u5920\u5c07\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u8f49\u63db\u70ba\u5c0d\u8c61\uff0c\u5f9e\u800c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u4ee5\u66f4\u76f4\u89c0\u7684\u65b9\u5f0f\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u500b\u6d41\u884c\u7684\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7684ORM\u5eab\u2014\u2014Entity Framework\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afentity-framework%ef%bc%9f-RjuqTiQXXB\">\u4ec0\u9ebc\u662fEntity Framework\uff1f<\/h2>\n<p>Entity Framework\uff08EF\uff09\u662f\u5fae\u8edf\u958b\u767c\u7684\u4e00\u500bORM\u6846\u67b6\uff0c\u4e3b\u8981\u7528\u65bc.NET\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5b83\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528.NET\u5c0d\u8c61\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u800c\u4e0d\u9700\u8981\u76f4\u63a5\u7de8\u5bebSQL\u67e5\u8a62\u3002EF\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecSQL Server\u3001MySQL\u3001PostgreSQL\u7b49\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u4e00\u500b\u975e\u5e38\u9748\u6d3b\u7684\u9078\u64c7\u3002<\/p>\n<h2 id=\"entity-framework%e7%9a%84%e4%b8%bb%e8%a6%81%e7%89%b9%e6%80%a7-RjuqTiQXXB\">Entity Framework\u7684\u4e3b\u8981\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff1a<\/strong>\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528LINQ\uff08\u8a9e\u8a00\u96c6\u6210\u67e5\u8a62\uff09\u4f86\u67e5\u8a62\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c0\u548c\u6613\u65bc\u7406\u89e3\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff1a<\/strong>EF\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u63d0\u4f9b\u8005\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002<\/li>\n<li><strong>\u81ea\u52d5\u5316\u6578\u64da\u5eab\u64cd\u4f5c\uff1a<\/strong>EF\u53ef\u4ee5\u81ea\u52d5\u751f\u6210\u6578\u64da\u5eab\u7d50\u69cb\uff0c\u4e26\u652f\u6301\u6578\u64da\u9077\u79fb\uff0c\u9019\u5927\u5927\u6e1b\u5c11\u4e86\u624b\u52d5\u64cd\u4f5c\u7684\u5de5\u4f5c\u91cf\u3002<\/li>\n<li><strong>\u5f37\u5927\u7684\u793e\u5340\u652f\u6301\uff1a<\/strong>\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684ORM\u5eab\uff0cEF\u64c1\u6709\u5927\u91cf\u7684\u6587\u6a94\u548c\u793e\u5340\u652f\u6301\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u627e\u5230\u89e3\u6c7a\u65b9\u6848\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8entity-framework-RjuqTiQXXB\">\u5982\u4f55\u4f7f\u7528Entity Framework<\/h2>\n<p>\u4f7f\u7528Entity Framework\u7684\u7b2c\u4e00\u6b65\u662f\u5b89\u88dd\u76f8\u61c9\u7684NuGet\u5305\u3002\u4ee5\u4e0b\u662f\u5b89\u88ddEF Core\u7684\u547d\u4ee4\uff1a<\/p>\n<pre><code>Install-Package Microsoft.EntityFrameworkCore<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u5275\u5efa\u4e00\u500b\u4e0a\u4e0b\u6587\u985e\uff0c\u9019\u500b\u985e\u5c07\u7528\u65bc\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4e0a\u4e0b\u6587\u985e\u793a\u4f8b\uff1a<\/p>\n<pre><code>public class MyDbContext : DbContext\n{\n    public DbSet&lt;Product&gt; Products { get; set; }\n\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n    {\n        optionsBuilder.UseSqlServer(&quot;YourConnectionStringHere&quot;);\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4e0a\u4e0b\u6587\u985e\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70baProducts\u7684DbSet\uff0c\u9019\u5c07\u5c0d\u61c9\u65bc\u6578\u64da\u5eab\u4e2d\u7684\u4e00\u500b\u8868\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528LINQ\u4f86\u9032\u884c\u6578\u64da\u67e5\u8a62\uff1a<\/p>\n<pre><code>using (var context = new MyDbContext())\n{\n    var products = context.Products.ToList();\n}<\/code><\/pre>\n<h2 id=\"entity-framework%e7%9a%84%e5%84%aa%e7%bc%ba%e9%bb%9e-RjuqTiQXXB\">Entity Framework\u7684\u512a\u7f3a\u9ede<\/h2>\n<h3 id=\"%e5%84%aa%e9%bb%9e-RjuqTiQXXB\">\u512a\u9ede<\/h3>\n<ul>\n<li>\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff1aORM\u7684\u4f7f\u7528\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u5c08\u6ce8\u65bc\u61c9\u7528\u908f\u8f2f\uff0c\u800c\u4e0d\u5fc5\u904e\u591a\u95dc\u6ce8\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/li>\n<li>\u6e1b\u5c11\u932f\u8aa4\uff1a\u81ea\u52d5\u751f\u6210\u7684SQL\u67e5\u8a62\u6e1b\u5c11\u4e86\u624b\u52d5\u7de8\u5bebSQL\u6642\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%bc%ba%e9%bb%9e-RjuqTiQXXB\">\u7f3a\u9ede<\/h3>\n<ul>\n<li>\u6027\u80fd\u554f\u984c\uff1a\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0cORM\u751f\u6210\u7684\u67e5\u8a62\u53ef\u80fd\u4e0d\u5982\u624b\u5beb\u7684SQL\u9ad8\u6548\u3002<\/li>\n<li>\u5b78\u7fd2\u66f2\u7dda\uff1a\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\uff0c\u7406\u89e3ORM\u7684\u5de5\u4f5c\u539f\u7406\u53ef\u80fd\u9700\u8981\u4e00\u5b9a\u7684\u6642\u9593\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-RjuqTiQXXB\">\u7e3d\u7d50<\/h2>\n<p>Entity Framework\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684ORM\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\u548c\u9748\u6d3b\u6027\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u7c21\u5316\u6578\u64da\u64cd\u4f5c\u9084\u662f\u81ea\u52d5\u5316\u6578\u64da\u5eab\u7ba1\u7406\uff0cEF\u90fd\u80fd\u5920\u6709\u6548\u5730\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684VPS\u670d\u52d9\u4f86\u904b\u884c\u61c9\u7528\u7a0b\u5e8f\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u7684\u8cc7\u8a0a\uff0c\u5c07\u6709\u52a9\u65bc\u60a8\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u7372\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u4e00\u500b\u6d41\u884c\u7684ORM\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u8b93\u6578\u64da\u7ba1\u7406\u66f4\u8f15\u9b06\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-194015","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=194015"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194015\/revisions"}],"predecessor-version":[{"id":194016,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194015\/revisions\/194016"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}