{"id":57962,"date":"2024-10-12T19:12:16","date_gmt":"2024-10-12T11:12:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57962\/"},"modified":"2024-10-12T19:12:16","modified_gmt":"2024-10-12T11:12:16","slug":"net-core-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%b0%a1%e4%bb%8b-net-core-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57962\/","title":{"rendered":".NET Core \u6578\u64da\u5eab\u9023\u63a5\u7c21\u4ecb (.net core \u6578\u64da\u5eab\u9023\u63a5)"},"content":{"rendered":"<h1 id=\"net-core-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%b0%a1%e4%bb%8b-lekkmOqCwb\">.NET Core \u6578\u64da\u5eab\u9023\u63a5\u7c21\u4ecb<\/h1>\n<p>.NET Core \u662f\u4e00\u500b\u8de8\u5e73\u53f0\u7684\u958b\u6e90\u6846\u67b6\uff0c\u5ee3\u6cdb\u7528\u65bc\u69cb\u5efa\u73fe\u4ee3\u5316\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u96a8\u8457\u6578\u64da\u9a45\u52d5\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\u4e0d\u65b7\u589e\u52a0\uff0c\u4e86\u89e3\u5982\u4f55\u5728 .NET Core \u4e2d\u9032\u884c\u6578\u64da\u5eab\u9023\u63a5\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u4ecb\u7d39 .NET Core \u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528 Entity Framework Core \u548c ADO.NET \u7684\u57fa\u672c\u6982\u5ff5\u53ca\u793a\u4f8b\u3002<\/p>\n<h2 id=\"1-net-core-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-lekkmOqCwb\">1. .NET Core \u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728 .NET Core \u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u4e3b\u8981\u4f9d\u8cf4\u65bc\u5169\u7a2e\u6280\u8853\uff1aEntity Framework Core \u548c ADO.NET\u3002\u9019\u5169\u8005\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u3002<\/p>\n<h3 id=\"1-1-entity-framework-core-lekkmOqCwb\">1.1 Entity Framework Core<\/h3>\n<p>Entity Framework Core\uff08EF Core\uff09\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u3001\u53ef\u64f4\u5c55\u7684 ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u80fd\u5920\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002EF Core \u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec SQL Server\u3001MySQL\u3001PostgreSQL \u7b49\u3002<\/p>\n<h4 id=\"%e4%bd%bf%e7%94%a8-ef-core-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e6%ad%a5%e9%a9%9f%ef%bc%9a-lekkmOqCwb\">\u4f7f\u7528 EF Core \u9032\u884c\u6578\u64da\u5eab\u9023\u63a5\u7684\u6b65\u9a5f\uff1a<\/h4>\n<ol>\n<li>\u5b89\u88dd EF Core \u5957\u4ef6\uff1a<\/li>\n<pre><code>dotnet add package Microsoft.EntityFrameworkCore.SqlServer<\/code><\/pre>\n<li>\u5275\u5efa\u6578\u64da\u4e0a\u4e0b\u6587\u985e\uff1a<\/li>\n<pre><code>public class ApplicationDbContext : DbContext\n{\n    public ApplicationDbContext(DbContextOptions options) : base(options) { }\n\n    public DbSet&lt;YourEntity&gt; YourEntities { get; set; }\n}<\/code><\/pre>\n<li>\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u5b57\u7b26\u4e32\uff1a<\/li>\n<pre><code>services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;\n    options.UseSqlServer(Configuration.GetConnectionString(\"DefaultConnection\")))<\/code><\/pre>\n<li>\u57f7\u884c\u6578\u64da\u9077\u79fb\uff1a<\/li>\n<pre><code>dotnet ef migrations add InitialCreate\ndotnet ef database update<\/code><\/pre>\n<\/ol>\n<h3 id=\"1-2-ado-net-lekkmOqCwb\">1.2 ADO.NET<\/h3>\n<p>ADO.NET \u662f\u4e00\u500b\u66f4\u5e95\u5c64\u7684\u6578\u64da\u8a2a\u554f\u6280\u8853\uff0c\u63d0\u4f9b\u4e86\u5c0d\u6578\u64da\u5eab\u7684\u76f4\u63a5\u64cd\u4f5c\u3002\u5b83\u9069\u5408\u9700\u8981\u9ad8\u6027\u80fd\u548c\u7d30\u7c92\u5ea6\u63a7\u5236\u7684\u5834\u666f\u3002<\/p>\n<h4 id=\"%e4%bd%bf%e7%94%a8-ado-net-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e6%ad%a5%e9%a9%9f%ef%bc%9a-lekkmOqCwb\">\u4f7f\u7528 ADO.NET \u9032\u884c\u6578\u64da\u5eab\u9023\u63a5\u7684\u6b65\u9a5f\uff1a<\/h4>\n<ol>\n<li>\u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/li>\n<pre><code>using (SqlConnection connection = new SqlConnection(\"YourConnectionString\"))\n{\n    connection.Open();\n    \/\/ \u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n}<\/code><\/pre>\n<li>\u57f7\u884c SQL \u547d\u4ee4\uff1a<\/li>\n<pre><code>using (SqlCommand command = new SqlCommand(\"SELECT * FROM YourTable\", connection))\n{\n    using (SqlDataReader reader = command.ExecuteReader())\n    {\n        while (reader.Read())\n        {\n            \/\/ \u8655\u7406\u6578\u64da\n        }\n    }\n}<\/code><\/pre>\n<\/ol>\n<h2 id=\"2-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-lekkmOqCwb\">2. \u6578\u64da\u5eab\u9023\u63a5\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u9023\u63a5\u6c60\uff1a<\/strong>\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u80fd\u5920\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\uff0c\u6e1b\u5c11\u9023\u63a5\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u8655\u7406\u7570\u5e38\uff1a<\/strong>\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\uff0c\u61c9\u8a72\u59a5\u5584\u8655\u7406\u7570\u5e38\u60c5\u6cc1\uff0c\u4ee5\u907f\u514d\u61c9\u7528\u7a0b\u5f0f\u5d29\u6f70\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7570\u6b65\u64cd\u4f5c\uff1a<\/strong>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u8003\u616e\u4f7f\u7528\u7570\u6b65\u65b9\u6cd5\u4f86\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u97ff\u61c9\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"3-%e7%b8%bd%e7%b5%90-lekkmOqCwb\">3. \u7e3d\u7d50<\/h2>\n<p>\u5728 .NET Core \u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u662f\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002\u7121\u8ad6\u662f\u4f7f\u7528 Entity Framework Core \u9084\u662f ADO.NET\uff0c\u958b\u767c\u8005\u90fd\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u3002\u900f\u904e\u6b63\u78ba\u7684\u914d\u7f6e\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684 .NET Core \u61c9\u7528\u7a0b\u5f0f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3.NET Core\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-57962","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57962","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57962"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57962\/revisions"}],"predecessor-version":[{"id":57963,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57962\/revisions\/57963"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}