{"id":753,"date":"2025-04-20T21:21:44","date_gmt":"2025-04-20T13:21:44","guid":{"rendered":"https:\/\/www.hyy.net\/?p=753"},"modified":"2025-04-20T21:21:44","modified_gmt":"2025-04-20T13:21:44","slug":"entity-framework-core-crud","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=753","title":{"rendered":"Entity Framework Core\u589e\u5220\u6539\u67e5"},"content":{"rendered":"<p>EntityFramework-Core\uff08EF Core\uff09\uff0c\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u9996\u9009ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5e76\u63d0\u4f9b\u4e86\u6d41\u7545\u7684API\u548c\u4ee3\u7801\u4f18\u5148\u7684\u5f00\u53d1\u65b9\u5f0f\u3002<\/p>\n<p>EntityFramework-Core\u4f18\u52bf<br \/>\n\u4ee3\u7801\u4f18\u5148\uff1a\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528C#\u4ee3\u7801\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\uff0cEF Core\u4f1a\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002<br \/>\n\u6570\u636e\u5e93\u8fc1\u79fb\uff1a\u652f\u6301\u6570\u636e\u5e93\u7684\u7248\u672c\u63a7\u5236\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u66f4\u65b0\u6570\u636e\u5e93\u7ed3\u6784\u3002<br \/>\n\u67e5\u8be2\u548c\u66f4\u65b0\uff1a\u63d0\u4f9bLINQ\u67e5\u8be2API\uff0c\u53ef\u4ee5\u4f7f\u7528C#\u8bed\u6cd5\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u540c\u65f6\u652f\u6301\u5f02\u6b65\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5e94\u7528\u54cd\u5e94\u901f\u5ea6\u3002<br \/>\n\u591a\u6570\u636e\u5e93\u652f\u6301\uff1a\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\uff08\u5982SQL Server\u3001MySQL\u3001PostgreSQL\u7b49\uff09\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n<p>\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u91cd\u8981\u6027<br \/>\n\u5728Web\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u6d89\u53ca\u5230\u6570\u636e\u7684\u5b58\u50a8\u3001\u68c0\u7d22\u3001\u66f4\u65b0\u548c\u5220\u9664\u3002\u9ad8\u6548\u3001\u5b89\u5168\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u6027\u80fd\u3001\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528ORM\u5de5\u5177\u5982EF Core\uff0c\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u51cf\u5c11SQL\u8bed\u53e5\u7684\u7f16\u5199\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u597d\u7684\u6570\u636e\u6a21\u578b\u7ba1\u7406\u548c\u4e8b\u52a1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h2>\u521b\u5efa\u6570\u636e\u5e93\u6a21\u578b<\/h2>\n<p>\u4f7f\u7528EF Core\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\u3002\u6a21\u578b\u901a\u5e38\u7531\u5b9e\u4f53\u7c7b\u8868\u793a\uff0c\u5b9e\u4f53\u7c7b\u4e2d\u7684\u5c5e\u6027\u5bf9\u5e94\u6570\u636e\u5e93\u8868\u4e2d\u7684\u5217\u3002<\/p>\n<pre><code>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u5b9e\u4f53\u7c7b\n\npublic class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public string Email { get; set; }\n    public DateTime CreatedAt { get; set; }\n}<\/code><\/pre>\n<h2>\u914d\u7f6eEF Core\u6570\u636e\u4e0a\u4e0b\u6587<\/h2>\n<p>\u5728\u9879\u76ee\u4e2d\u4f7f\u7528EF Core\uff0c\u9700\u8981\u5728DbContext\u4e2d\u5b9a\u4e49\u6570\u636e\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u9700\u8981\u5b89\u88c5<br \/>\nMicrosoft.EntityFrameworkCore\u548cMicrosoft.EntityFrameworkCore.SqlServer<\/p>\n<pre><code>using Microsoft.EntityFrameworkCore;\n\nnamespace ConsoleApp4\n{\n    internal class Program\n    {\n        static void Main(string[] args)\n        {\n            Console.WriteLine(&quot;Hello, World!&quot;);\n        }\n    }\n}\n\npublic class ApplicationDbContext : DbContext\n{\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n    {\n        optionsBuilder.UseSqlServer(\n            @&quot;Server=(localdb)\\MSSQLLocalDB;Initial Catalog=mydb&quot;);\n    }\n\n    public DbSet&lt;User&gt; Users { get; set; }\n}\n\npublic class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public string Email { get; set; }\n    public DateTime CreatedAt { get; set; }\n}\n<\/code><\/pre>\n<h2>\u521b\u5efa\u6570\u636e\u5e93<\/h2>\n<p>\u4e0a\u9762\u521b\u5efa\u4e86\u6a21\u578b\uff0c\u63a5\u4e0b\u6765\u4f7f\u7528\u8fc1\u79fb\u6765\u521b\u5efa\u6570\u636e\u5e93\u3002\u8fd9\u91cc\u9700\u8981\u501f\u52a9\u4e00\u4e9b\u5de5\u5177\uff0c\u6709\u5173\u7684\u4e24\u4e2a\u5de5\u5177\u96c6\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09.NET Core \u547d\u4ee4\u884c\u63a5\u53e3 (CLI) \u5de5\u5177\u53ef\u7528\u4e8e Windows\u3001Linux \u6216 macOS\u3002 \u8fd9\u4e9b\u547d\u4ee4\u4ee5 dotnet ef \u5f00\u5934\u3002\u5c06 dotnet ef \u5b89\u88c5\u4e3a\u5168\u5c40\u5de5\u5177\u6216\u672c\u5730\u5de5\u5177\u3002 \u5927\u591a\u6570\u5f00\u53d1\u4eba\u5458\u504f\u5411\u4e8e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06 dotnet ef \u5b89\u88c5\u4e3a\u5168\u5c40\u5de5\u5177\uff1a<br \/>\ndotnet tool install --global dotnet-ef<\/p>\n<p>2\uff09\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0 (PMC) \u5de5\u5177\u5728 Windows \u4e0a\u7684 Visual Studio \u4e2d\u8fd0\u884c\u3002 \u8fd9\u4e9b\u547d\u4ee4\u4ee5\u52a8\u8bcd\u5f00\u5934\uff0c\u4f8b\u5982 Add-Migration\u3001Update-Database\u3002\u3000\u83b7\u53d6\u9002\u7528\u4e8e EF Core \u7684\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0\u5de5\u5177\uff0c\u9700\u8981\u5b89\u88c5 Microsoft.EntityFrameworkCore.Tools \u5305\u3002<br \/>\nInstall-Package Microsoft.EntityFrameworkCore.Tools<\/p>\n<p>\u5b8c\u6574\u7684\u8fc1\u79fb\u547d\u4ee4\u8fc7\u7a0b\u5982\u4e0b\uff1a<br \/>\n1.\u5b89\u88c5 dotnet ef<\/p>\n<p>2.\u5b89\u88c5\u547d\u4ee4\u884c EF Core \u5f00\u53d1\u7684\u5de5\u5177\u5957\u4ef6Microsoft.EntityFrameworkCore.Design\u3002<\/p>\n<p>3.\u6dfb\u52a0\u7b2c\u4e00\u6b21\u8fc1\u79fb<\/p>\n<pre><code>dotnet ef migrations add initialcreate<\/code><\/pre>\n<pre><code>dotnet ef migrations add initialcreate\nBuild started...\nBuild succeeded.\nDone. To undo this action, use &#039;ef migrations remove&#039;<\/code><\/pre>\n<p>\u6ce8\uff1a\u5982\u679c\u4f7f\u7528\u9ad8\u7248\u672c\u7684SQL SERVER\u6216\u8005SQL SERVER EXPRESS\u4f1a\u63d0\u793a\u9519\u8bef\uff1a\u201c\u8bc1\u4e66\u94fe\u662f\u7531\u4e0d\u53d7\u4fe1\u4efb\u7684\u9881\u53d1\u673a\u6784\u9881\u53d1\u7684\u201d\uff0c\u89e3\u51b3\u65b9\u6cd5\u662f\u6dfb\u52a0 ;Trust Server Certificate=true \u5230\u8fde\u63a5\u5b57\u7b26\u4e32\u3002 \u8fd9\u5c06\u5f3a\u5236\u5ba2\u6237\u7aef\u5728\u4e0d\u8fdb\u884c\u9a8c\u8bc1\u7684\u60c5\u51b5\u4e0b\u4fe1\u4efb\u8bc1\u4e66\u3002<\/p>\n<p>4.\u5c06\u8fc1\u79fb\u5e94\u7528\u5230\u6570\u636e\u5e93<\/p>\n<pre><code>dotnet ef database update\nBuild started...\nBuild succeeded.\nApplying migration &#039;20250420124636_initialcreate&#039;.\nDone.<\/code><\/pre>\n<p>\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u53d1\u73b0\u5df2\u7ecf\u65b0\u5efa\u4e86\u6570\u636e\u5e93\u548c\u8868\u3002<\/p>\n<h2>\u589e\u5220\u6539\u67e5<\/h2>\n<pre><code>using Microsoft.EntityFrameworkCore;\n\nnamespace ConsoleApp4\n{\n    internal class Program\n    {\n        static void Main(string[] args)\n        {\n            var dbContext = new ApplicationDbContext();\n            var userinfo = dbContext.Users;\n\n            Console.WriteLine(&quot;Inserting&quot;);\n            dbContext.Users.Add(new User { Name = &quot;Tom&quot;,  Email = &quot;dese@123.com&quot; });\n            dbContext.SaveChanges();\n            Console.WriteLine(&quot;Inserting complete.&quot;);\n\n            Console.WriteLine(&quot;Querying&quot;);\n            var user = dbContext.Users.OrderBy(b =&gt;b.Id).FirstOrDefault();\n            Console.WriteLine(user.Name);\n            Console.WriteLine(user.Email);\n            Console.WriteLine(&quot;Querying complete.&quot;);\n\n            Console.WriteLine(&quot;Updating&quot;);\n            user.Email = &quot;234@gamil.com&quot;;\n            dbContext.SaveChanges();\n            Console.WriteLine(user.Name);\n            Console.WriteLine(user.Email);\n            Console.WriteLine(&quot;Updating complete.&quot;);\n\n            Console.WriteLine(&quot;Delete&quot;);\n            dbContext.Remove(user);\n            dbContext.SaveChanges();\n            Console.WriteLine(&quot;Delete complete.&quot;);\n\n        }\n    }\n}\n\npublic class ApplicationDbContext : DbContext\n{\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n    {\n        optionsBuilder.UseSqlServer(\n            @&quot;Server=(localdb)\\MSSQLLocalDB;Initial Catalog=mydb&quot;);\n    }\n\n    public DbSet&lt;User&gt; Users { get; set; }\n}\n\npublic class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public string Email { get; set; }\n    public DateTime CreatedAt { get; set; }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>EntityFramework-Core\uff08EF Core\uff09\uff0c\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u9996\u9009ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5e76\u63d0\u4f9b\u4e86\u6d41\u7545\u7684API\u548c\u4ee3\u7801\u4f18\u5148\u7684\u5f00\u53d1\u65b9\u5f0f\u3002 EntityFramework-Core\u4f18\u52bf \u4ee3\u7801\u4f18\u5148\uff1a\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528C#\u4ee3\u7801\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\uff0cEF Core\u4f1a\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002 \u6570\u636e\u5e93\u8fc1\u79fb\uff1a\u652f\u6301\u6570\u636e\u5e93\u7684\u7248\u672c\u63a7\u5236\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u66f4\u65b0\u6570\u636e\u5e93\u7ed3\u6784\u3002 \u67e5\u8be2\u548c\u66f4\u65b0\uff1a\u63d0\u4f9bLINQ\u67e5\u8be2API\uff0c\u53ef\u4ee5\u4f7f\u7528C#\u8bed\u6cd5\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u540c\u65f6\u652f\u6301\u5f02\u6b65\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5e94\u7528\u54cd\u5e94\u901f\u5ea6\u3002 \u591a\u6570\u636e\u5e93\u652f\u6301\uff1a\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\uff08\u5982SQL Server\u3001MySQL\u3001PostgreSQL\u7b49\uff09\u65e0\u7f1d\u96c6\u6210\u3002 \u6570\u636e\u5e93\u64cd\u4f5c\u7684\u91cd\u8981\u6027 \u5728Web\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u6d89\u53ca\u5230\u6570\u636e\u7684\u5b58\u50a8\u3001\u68c0\u7d22\u3001\u66f4\u65b0\u548c\u5220\u9664\u3002\u9ad8\u6548\u3001\u5b89\u5168\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u6027\u80fd\u3001\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528ORM\u5de5\u5177\u5982EF Core\uff0c\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u51cf\u5c11SQL\u8bed\u53e5\u7684\u7f16\u5199\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u597d\u7684\u6570\u636e\u6a21\u578b\u7ba1\u7406\u548c\u4e8b\u52a1\u5904\u7406\u80fd\u529b\u3002 \u521b\u5efa\u6570\u636e\u5e93\u6a21\u578b \u4f7f\u7528EF Core\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\u3002\u6a21\u578b\u901a\u5e38\u7531\u5b9e\u4f53\u7c7b\u8868\u793a\uff0c\u5b9e\u4f53\u7c7b\u4e2d\u7684\u5c5e\u6027\u5bf9\u5e94\u6570\u636e\u5e93\u8868\u4e2d\u7684\u5217\u3002 \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5b9e\u4f53\u7c7b public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public DateTime CreatedAt { get; set; } } \u914d\u7f6eEF Core\u6570\u636e\u4e0a\u4e0b\u6587 \u5728\u9879\u76ee\u4e2d\u4f7f\u7528EF Core\uff0c\u9700\u8981\u5728DbContext\u4e2d\u5b9a\u4e49\u6570\u636e\u4e0a\u4e0b\u6587\u3002 \u9700\u8981\u5b89\u88c5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-csharp","category-efcore"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=753"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/753\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}