{"id":100008,"date":"2023-12-08T21:09:08","date_gmt":"2023-12-08T21:09:08","guid":{"rendered":"https:\/\/martinphysics.club\/?p=100008"},"modified":"2023-12-08T21:09:08","modified_gmt":"2023-12-08T21:09:08","slug":"usingdotnetwithefcoremarktheweatherinyourcity","status":"publish","type":"post","link":"https:\/\/martinphysics.club\/?p=100008","title":{"rendered":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14"},"content":{"rendered":"<h2 id=\"\u9879\u76ee\u7b80\u8ff0\">\u9879\u76ee\u7b80\u8ff0<\/h2>\n<p>\u8fd9\u4e2a\u9879\u76ee\u662f\u6211\u5728\u5b66\u4e60.Net Core\u7f16\u7a0b\u7684\u65f6\u5019\u7528\u5230\u7684\u7b80\u5355\u4f8b\u5b50\uff0c\u6574\u4f53\u5c31\u662f\u901a\u8fc7Get\u8bf7\u6c42\u83b7\u53d6\u67d0\u5730\u533a\u7684\u5929\u6c14\u4fe1\u606f\uff0c\u7ecf\u8fc7json\u53cd\u5411\u5e8f\u5217\u5316\u540e\u5199\u5165\u5230\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u8d77\u6765\u3002\u901a\u8fc7.net6\u7684EntityFramework\u6211\u4eec\u53ef\u4ee5\u5f88\u4f18\u96c5\u5730\u4e3a\u8fd9\u4e2a\u5c0f\u9879\u76ee\u8fdb\u884cCodeFirst\u7684\u8bbe\u8ba1\uff08\u6cbf\u7528\u6768\u4e2d\u79d1\u8001\u5e08\u7684\u4e00\u4e2a\u89c2\u70b9\uff1a&ldquo;\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5bf9\u7a0b\u5e8f\u5458\u6765\u8bb2\u53ea\u662f\u4e00\u4e2a\u5b58\u653e\u6570\u636e\u6a21\u578b\u7684\u5a92\u4ecb\uff0c\u7406\u60f3\u72b6\u6001\u4e0b\u7a0b\u5e8f\u5458\u4e0d\u7528\u5173\u5fc3\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002&rdquo;\uff09\u8fd9\u4e2a\u9879\u76ee\u5c06\u4f1a\u7528EntityFramework\u8fdb\u884c\u6570\u636e\u5e93\u7684CURD\u64cd\u4f5c\uff0c\u4efb\u52a1\u76ee\u6807\uff1a<\/p>\n<ul>\n<li>\u83b7\u53d6\u548c\u98ce\u5929\u6c14\u63a5\u53e3\u7684\u6570\u636e<\/li>\n<li>\u89e3\u6790\u6570\u636e\u5e76\u8fdb\u884c\u6570\u636e\u5e93\u63d2\u5165\u52a8\u4f5c<\/li>\n<\/ul>\n<h2 id=\"\u914d\u7f6e\u6761\u4ef6\">\u914d\u7f6e\u6761\u4ef6<\/h2>\n<ul>\n<li>DotNetCore\u63a7\u5236\u53f0\u5e94\u7528(.Net 6.0)<\/li>\n<li>Microsoft SQL Server 2022<\/li>\n<li>Nuget Packages\n<ul>\n<li>EntityFrameworkCore.SqlServer(version6.0.25)<\/li>\n<li>EntityFrameworkCore.Tools(version6.0.25)<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/dev.qweather.com\/\">\u548c\u98ce\u5929\u6c14Qweather\u5f00\u53d1\u670d\u52a1<\/a> \uff08\u53ef\u514d\u8d39\u8ba2\u9605\uff09<\/li>\n<\/ul>\n<h4 id=\"\u5173\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\">\u5173\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93:<\/h4>\n<p>\u5bf9\u4e8eSQL Server\u6570\u636e\u5e93\u5b89\u88c5\u914d\u7f6e\uff0c\u8bf7\u81ea\u884c\u8c37\u6b4c\u3002<\/p>\n<h4 id=\"\u5173\u4e8e\u548c\u98ce\u5929\u6c14qweather\u5f00\u53d1\u670d\u52a1\">\u5173\u4e8e\u548c\u98ce\u5929\u6c14Qweather\u5f00\u53d1\u670d\u52a1:<\/h4>\n<p>\u548c\u98ce\u5929\u6c14\u514d\u8d39\u8ba2\u9605\u7684\u63a5\u53e3\u5730\u5740\u662f:<\/p>\n<pre class=\"language-xml\"><code>https:\/\/devapi.qweather.com\/v7\/weather\/3d?location={LocationCode}&amp;key={Key}<\/code><\/pre>\n<p>\u901a\u8fc7\u7b80\u5355\u7684Get\u8bf7\u6c42\u5373\u53ef\u83b7\u5f97\u5929\u6c14\u4fe1\u606f\u7ed3\u6784\u4f53\uff0c\u4f46\u6ce8\u610f\u548c\u98ce\u5929\u6c14\u5f00\u53d1\u670d\u52a1\u7684API\u5747\u4f7f\u7528Gzip\u8fdb\u884c\u4e86\u538b\u7f29\uff0c\u4ee3\u7801\u4e2d\u8981\u8fdb\u884c\u89e3\u538b\u5904\u7406\u3002\u8be6\u7ec6\u4fe1\u606f<a href=\"https:\/\/dev.qweather.com\/docs\/configuration\/api-config\/\">\u53c2\u8003\u5b98\u65b9\u7684\u5f00\u53d1\u6587\u6863<\/a>\u3002\u540c\u65f6\u57ce\u5e02\u4ee3\u7801\u53ef\u4ee5\u5728<a href=\"https:\/\/github.com\/qwd\/LocationList\/tree\/master\">\u8fd9\u4e2aGithub\u4ed3\u5e93<\/a>\u5f53\u4e2d\u67e5\u8be2\u5f97\u5230\u3002\u63a5\u53e3\u8fd4\u56de\u7684json\u5982\u4e0b<\/p>\n<pre class=\"language-json\"><code>{\n  \"code\": \"200\",\n  \"updateTime\": \"2023-12-07T14:47+08:00\",\n  \"fxLink\": \"https:\/\/www.qweather.com\/weather\/beijing-101010100.html\",\n  \"now\": {\n    \"obsTime\": \"2023-12-07T14:38+08:00\",\n    \"temp\": \"8\",\n    \"feelsLike\": \"5\",\n    \"icon\": \"504\",\n    \"text\": \"\u6d6e\u5c18\",\n    \"wind360\": \"30\",\n    \"windDir\": \"\u4e1c\u5317\u98ce\",\n    \"windScale\": \"2\",\n    \"windSpeed\": \"10\",\n    \"humidity\": \"30\",\n    \"precip\": \"0.0\",\n    \"pressure\": \"1000\",\n    \"vis\": \"6\",\n    \"cloud\": \"0\",\n    \"dew\": \"-8\"\n  },\n  \"refer\": {\n    \"sources\": [\n      \"QWeather\"\n    ],\n    \"license\": [\n      \"CC BY-SA 4.0\"\n    ]\n  }\n}<\/code><\/pre>\n<h2 id=\"\u4ee3\u7801\u6846\u67b6\u8bbe\u8ba1\">\u4ee3\u7801\u6846\u67b6\u8bbe\u8ba1<\/h2>\n<h3 id=\"\u521b\u5efa\u57fa\u7840\u4ee3\u7801\u6846\u67b6\">\u521b\u5efa\u57fa\u7840\u4ee3\u7801\u6846\u67b6<\/h3>\n<p>\u901a\u8fc7Visual Studio 2022\u65b0\u5efa\u4e00\u4e2aC#\u63a7\u5236\u53f0\u5e94\u7528\uff0c\u6ce8\u610f\u4e0d\u8981\u9009.Net Framework\u3002\u6846\u67b6\u9009\u62e9.Net 6.0\uff08\u957f\u671f\u652f\u6301\uff09\u3002\u5f53\u7136\u5982\u679c\u60f3\u5b66\u4e60.NET 8\u4e5f\u53ef\u4ee5\u79ef\u6781\u5c1d\u8bd5~<\/p>\n<p>\u5bf9\u4e8e\u8fd9\u6b21\u4efb\u52a1\u7528\u5230\u7684\u6570\u636e\u6a21\u578bModels\u6709\uff1a<\/p>\n<ul>\n<li>\u548c\u98ce\u5929\u6c14\u6570\u636e\u6a21\u578b<\/li>\n<li>\u57ce\u5e02\u6635\u79f0\u53ca\u5176\u5bf9\u5e94\u7684\u4ee3\u53f7<\/li>\n<\/ul>\n<h4 id=\"\u521b\u5efa\u57ce\u5e02\u4ee3\u7801\u5b57\u5178\">\u521b\u5efa\u57ce\u5e02\u4ee3\u7801\u5b57\u5178<\/h4>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u89e3\u51b3\u65b9\u6848\u76ee\u5f55\u4e0b\u65b0\u5efaModel\u6587\u4ef6\u5939(\u672c\u6587\u5047\u8bbe\u4f60\u7684\u89e3\u51b3\u65b9\u6848\u6635\u79f0\u662fWeatherCollectorConsole)\uff0c\uff0c\u5728Model\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaCityCode\u6587\u4ef6\uff0c\u6211\u4eec\u7528Dictionary\u5b58\u653estring\u5bf9\u8c61\u7684\u57ce\u5e02\u6635\u79f0\uff0c\u7528int\u5bf9\u8c61\u5b58\u653e\u5bf9\u5e94\u57ce\u5e02\u4ee3\u7801\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"language-csharp\"><code>namespace WeatherCollectorConsole.Models\n{\n    public class CityCode\n    {\n        public readonly Dictionary&lt;string, int&gt;  cityCodeDict = new()\n        {\n                {\"\u5317\u4eac\", 101010100},\n                {\"\u4e0a\u6d77\", 101020100}\n            };\n    }\n}<\/code><\/pre>\n<h4 id=\"\u521b\u5efa\u548c\u98ce\u5929\u6c14\u63a5\u53e3\u7684\u6570\u636e\u6a21\u578b\">\u521b\u5efa\u548c\u98ce\u5929\u6c14\u63a5\u53e3\u7684\u6570\u636e\u6a21\u578b<\/h4>\n<p>\u5bf9\u4e8e\u548c\u98ce\u5929\u6c14\u7684json\u683c\u5f0f\uff0c\u6211\u4eec\u5728Model\u6587\u4ef6\u5939\u4e2d\u53e6\u884c\u65b0\u5efaQWeatherjson\u6587\u4ef6\uff0c\u901a\u8fc7\u7c7b\u53bb\u5efa\u7acb\u8d77\u6570\u636e\u6a21\u578b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"language-csharp\"><code>namespace WeatherCollectorConsole.Models\n{\n    public class QWeatherAPIjson\n    {\n        public string code { get; set; }\n        public string updateTime { get; set; }\n        public string fxLink { get; set; }\n        public WeatherDetails now { get; set; }\n        public Refer refer { get; set; }\n    }\n    public class WeatherDetails\n    {\n        public string obsTime { get; set; }\n        public string temp { get; set; }\n        public string feelsLike { get; set; }\n        public string icon { get; set; }\n        public string text { get; set; }\n        public string wind360 { get; set; }\n        public string windDir { get; set; }\n        public string windScale { get; set; }\n        public string windSpeed { get; set; }\n        public string humidity { get; set; }\n        public string precip { get; set; }\n        public string pressure { get; set; }\n        public string vis { get; set; }\n        public string cloud { get; set; }\n        public string dew { get; set; }\n    }\n\n    public class Refer\n    {\n        public List&lt;string&gt; sources { get; set; }\n        public List&lt;string&gt; license { get; set; }\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u5230\uff0c\u8fd9\u91cc\u7c7b\u4e2d\u7684\u6bcf&ldquo;\u9879&rdquo;\u7684\u540d\u79f0\u90fd\u5e94\u8be5\u548cjson\u7684\u7269\u4ef6\u540d\u79f0\u4e00\u4e00\u5bf9\u5e94\uff0c\u8fd9\u4f1a\u5728\u540e\u9762\u5b58\u653e\u6570\u503c\u63d0\u4f9b\u5927\u5927\u7684\u65b9\u4fbf\u3002<\/p>\n<h3 id=\"\u914d\u7f6eentityframework\u73af\u5883\">\u914d\u7f6eEntityFramework\u73af\u5883<\/h3>\n<p>\u5bf9\u4e8e\u672c\u6587\u7684.NET6.0\u6846\u67b6\uff0c\u8bf7\u5728\u89e3\u51b3\u65b9\u6848\u5904\u53f3\u952e-\u9009\u53d6&ldquo;\u7ba1\u7406Nuget\u7a0b\u5e8f\u5305&rdquo;\uff0c\u68c0\u7d22\u4e0b\u5217nuget\u5305\u6635\u79f0\u53ca\u7248\u672c\u8fdb\u884c\u5b89\u88c5<\/p>\n<ul>\n<li>EntityFrameworkCore.SqlServer(version6.0.25)<\/li>\n<li>EntityFrameworkCore.Tools(version6.0.25)<\/li>\n<\/ul>\n<p>\u5982\u679c\u662f.Net 8.0\u7684\u9879\u76ee\uff0c\u5c31\u5b89\u88c5\u5bf9\u5e94\u7684version8.x \u914d\u7f6eCodeFirst\u7684EntityFramework\u73af\u5883\u5927\u6982\u9700\u8981\u4e09\u7c7b\u4ee3\u7801\uff1a<\/p>\n<ul>\n<li>\u6570\u636e\u8868\u7c7b\u7684\u58f0\u660e<\/li>\n<li>\u6570\u636e\u8868\u7684\u914d\u7f6e<\/li>\n<li>\u6570\u636e\u5e93\u7684\u64cd\u4f5c<\/li>\n<\/ul>\n<h4 id=\"\u6570\u636e\u8868weathertable\u7684\u58f0\u660e\">\u6570\u636e\u8868WeatherTable\u7684\u58f0\u660e<\/h4>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728Models\u6587\u4ef6\u4e0b\u4e2d\u5efa\u7acb\u58f0\u660e(\u56e0\u4e3a\u672c\u8d28\u4e0a\u90fd\u662f\u4e00\u79cd\u6570\u636e\u6a21\u578b~)\uff0c\u65b0\u5efaWeatherTable.cs\u6587\u4ef6\uff0c\u952e\u5165\u4ee3\u7801\uff1a<\/p>\n<pre class=\"language-csharp\"><code>using System.ComponentModel.DataAnnotations;\n\nnamespace WeatherCollectorConsole.Models\n{\n    public class WeatherTable\n    {\n        [Key]\n        public long Id { get; set; }\n        public DateTime DataUpdateTime { get; set; }\n        public string CityName { get; set; }\n        public DateTime ObsTime { get; set; }\n        public int? Temp { get; set; }\n        public int? TempFeelsLike { get; set; }\n        public string? WeatherText { get; set; }\n        public int? Wind360 { get; set; }\n        public string? WindDir { get; set; }\n        public int? WindScale { get; set; }\n        public int? WindSpeed { get; set; }\n        public int? Humidity { get; set; }\n        public double? RainPrecip { get; set; }\n        public int? Pressure { get; set; }\n        public int? Vis { get; set; }\n        public int? Cloud { get; set; }\n        public int? Dew { get; set; }\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u5230\uff1a<\/p>\n<ul>\n<li>\u91c7\u7528\u4e86DataAnnotations\u6807\u6ce8\u81ea\u589e\u79cd\u5b50\u4e3b\u952ekey<\/li>\n<li>\u7c7b\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u7c7b\u578b\u4f1a\u901a\u8fc7EF(EntityFrameworkd\u7684\u7f29\u5199)\u5bf9\u6570\u636e\u5e93\u5bf9\u5e94\u7684\u53d8\u91cf\u7c7b\u578b\u8fdb\u884c\u8bbe\u8ba1\uff0c\u5f53\u7136\u53ef\u4ee5\u540e\u7eed\u6211\u4eec\u53ef\u4ee5\u5fae\u8c03<\/li>\n<\/ul>\n<h5 id=\"\u6570\u636e\u8868\u7684\u914d\u7f6e\">\u6570\u636e\u8868\u7684\u914d\u7f6e<\/h5>\n<p>\u6570\u636e\u8868\u7684\u914d\u7f6e\u5efa\u8bae\u65b0\u5efaConfig\u4ee3\u7801\u6587\u4ef6\uff0c\u540e\u7eed\u8fdb\u884c\u4ee3\u7801\u7684\u7ef4\u62a4\u3002\u65b0\u5efaWeatherTableConfig.cs\u6587\u4ef6\uff0c\u952e\u5165\u4ee3\u7801\uff1a<\/p>\n<pre class=\"language-csharp\"><code>using Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Metadata.Builders;\nusing WeatherCollectorConsole.Models;\nnamespace WeatherCollectorConsole\n{\n    class WeatherTableConfig:IEntityTypeConfiguration&lt;WeatherTable&gt;\n    {\n        public void Configure(EntityTypeBuilder&lt;WeatherTable&gt; builder)\n        {\n            builder.ToTable(\"WeatherTable\");\n        }\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u662fEF\u901a\u8fc7WeatherTable\u7c7b\u786e\u5b9a\u597d\u6570\u636e\u8868\u7684\u8bbe\u8ba1\uff0c\u6240\u4ee5\u5c31\u79f0\u547c\u4e3a&ldquo;\u6570\u636e\u8868\u7684\u914d\u7f6e\u4ee3\u7801&rdquo;\u3002EF\u5bf9\u6570\u636e\u8868\u7684\u7ea6\u5b9a\u662f\u4ee5\u4fdd\u8bc1\u4e0d\u4f1a\u7206\u7684\u524d\u63d0\u4e0b\u8fdb\u884c\u7684\uff0c\u5982string\u5bf9\u5e94\u7684nvchar\u957f\u5ea6\u591a\u5c11\u597d\u5462\uff1f\u80af\u5b9a\u662fmax\u4e0d\u4f1a\u7206\u554a~\u4e3a\u4e86\u73af\u4fdd\u4e3b\u4e49\uff0c\u6211\u8fd9\u91cc\u4f1a\u5bf9\u57ce\u5e02\u540d\u79f0CityName\u3001\u5929\u6c14\u72b6\u51b5WeatherText\u3001\u98ce\u5411WindDir\u4e09\u4e2a\u9879\u76ee\u9650\u5236\u957f\u5ea6\uff0c\u5e76\u5bf9\u6570\u636e\u66f4\u65b0\u65f6\u95f4DataUpdateTime(\u5f53\u4f5c\u6570\u636e\u5199\u5165\u6570\u636e\u5e93\u7684\u65f6\u95f4\uff0c\u975e\u6570\u636e\u89c2\u6d4b\/\u68c0\u6d4b\u7684\u65f6\u95f4)\u8981\u6c42\u4e0d\u80fd\u4e3a\u7a7a\u3002\u5c06\u4f1a\u5bf9builder\u989d\u5916\u52a0\u5165\u5c5e\u6027\u8981\u6c42\uff1a<\/p>\n<pre class=\"language-csharp\"><code>builder.Property(b=&gt;b.CityName).HasMaxLength(25).IsRequired();\nbuilder.Property(b=&gt;b.DataUpdateTime).IsRequired();\nbuilder.Property(b=&gt;b.WeatherText).HasMaxLength(30);\nbuilder.Property(b =&gt; b.WindDir).HasMaxLength(30);<\/code><\/pre>\n<p>\u6574\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"language-csharp\"><code>using Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Metadata.Builders;\nusing WeatherCollectorConsole.Models;\nnamespace WeatherCollectorConsole\n{\n    class WeatherTableConfig:IEntityTypeConfiguration&lt;WeatherTable&gt;\n    {\n        public void Configure(EntityTypeBuilder&lt;WeatherTable&gt; builder)\n        {\n            builder.ToTable(\"WeatherTable\");\n            builder.Property(b=&gt;b.CityName).HasMaxLength(25).IsRequired();\n            builder.Property(b=&gt;b.DataUpdateTime).IsRequired();\n            builder.Property(b=&gt;b.WeatherText).HasMaxLength(30);\n            builder.Property(b =&gt; b.WindDir).HasMaxLength(30);\n        }\n    }\n}<\/code><\/pre>\n<h4 id=\"\u6570\u636e\u5e93\u7269\u4ef6\u7684\u5efa\u7acb\">\u6570\u636e\u5e93\u7269\u4ef6\u7684\u5efa\u7acb<\/h4>\n<p>\u5728\u89e3\u51b3\u65b9\u6848\u4e2d\u65b0\u5efaWeatherDBContext.cs\u6587\u4ef6\uff0c\u952e\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"language-csharp\"><code>using Microsoft.EntityFrameworkCore;\nusing WeatherCollectorConsole.Models;\n\nnamespace WeatherCollectorConsole\n{\n    class WeatherDBContext:DbContext\n    {\n        public DbSet&lt;WeatherTable&gt; WeatherTables { get;set;}\n\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n        {\n            base.OnConfiguring(optionsBuilder);\n            optionsBuilder.UseSqlServer(\"Server=serveraddress,port;Database=database;User ID=username;Password=password\");\n        }\n\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\n        {\n            base.OnModelCreating(modelBuilder);\n            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);\n        }\n    }\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u7ec3\u624b\u7a0b\u5e8f\u6211\u4eec\u6682\u4e14\u901a\u8fc7hardcore\u5e76\u660e\u6587\u4fdd\u5b58\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32&hellip;\u800c\u4e14\u6211\u5f53\u4f60\u662f\u5df2\u7ecf\u89e3\u51b3\u597d\u6570\u636e\u5e93\u7684\u8fde\u63a5\u95ee\u9898\u7684\u4e86&hellip; \u7b80\u5355\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801\u7684\u610f\u4e49\uff1a<\/p>\n<ul>\n<li>\u58f0\u660eWeatherDBContext\u8fd9\u4e2a\u7c7b\u7ee7\u627fEF\u7684DbContext<\/li>\n<li>WeatherDBContext\u4e2d\u901a\u8fc7DbSet\u52a0\u8f7d\u4f60\u5b9a\u5236\u7684\u5404\u79cd\u6570\u636e\u8868\u7684\u7ed3\u6784<\/li>\n<li>OnModelCreating\u65b9\u6cd5\u4e2d\u7684ApplyConfigurationsFromAssembly\u53ef\u4ee5\u81ea\u52a8\u83b7\u53d6DbSet\u4e2d\u7684T\u7684\u7c7b\u578b<\/li>\n<li>OnConfiguring\u7528\u4e8e\u5f00\u542f\u4f60\u7684\u6570\u636e\u5e93\u7684\u8fde\u63a5..<\/li>\n<\/ul>\n<h4 id=\"codefirtst\">CodeFirtst\uff01<\/h4>\n<p>CodeFirtst\u662f\u6700\u7cbe\u9ad3\u7684\u64cd\u4f5c~\u5bf9\u4e8e\u8fd9\u79cd\u975e\u751f\u4ea7\u578b\u7684\u5c0f\u9879\u76ee\u53ef\u80fd\u4f1a\u6709\u65e0\u6570\u6539\u52a8\uff0c\u6211\u4eec\u901a\u8fc7\u4fee\u6539WeatherTable\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u5feb\u901f\u540c\u6b65\u4fee\u6539\u6570\u636e\u5e93\u7684\u6570\u636e\u7c7b\u578b\uff0c\u65e0\u8bba\u589e\u51cf\u8fd8\u662f\u5220\u5e93\u8dd1\u8def\u90fd\u4e0d\u7528\u9ebb\u70e6\u5176\u4ed6\u7684\u540c\u4e8b~\u5177\u4f53\u6211\u4eec\u4f1a\u7528\u5230EntityFrameworkCore.Tools\u7684Migration\u8fdb\u884c\u64cd\u4f5c\uff1a \u9996\u5148\u627e\u5230Visual Studio\u7684\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\u5728&ldquo;\u89c6\u56fe-\u5176\u4ed6\u7a97\u53e3-\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0&rdquo;\u53ef\u4ee5\u627e\u5230\u3002 \u8bf7\u6ce8\u610fMigration\u8fd9\u4e2a&ldquo;\u8fc1\u79fb&rdquo;\u64cd\u4f5c\u9700\u8981\u5f53\u524d\u4ee3\u7801\u80fd\u591f\u6b63\u5e38\u901a\u8fc7\u7f16\u8bd1\u3002\u6392\u9664\u4ee3\u7801\u7684\u9519\u8bef\u540e\u5728\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0\u952e\u5165\u547d\u4ee4\uff1a<\/p>\n<pre class=\"language-bash\"><code>Add-Migration MigrationName<\/code><\/pre>\n<p>\u8bf7\u628aMigrationName\u6539\u6210\u5b9e\u9645\u6709\u610f\u4e49\u7684remark\uff0cMigration\u64cd\u4f5c\u4f1a\u5728Migration\u6587\u4ef6\u5939\u4e0b\u751f\u6210Datetime_MigrationName.cs\u6587\u4ef6\u7528\u4e8e\u533a\u5206\u6bcf\u6b21&ldquo;\u8fc1\u79fb&rdquo;\uff0c\u5f53Migration\u547d\u4ee4\u6b63\u5e38\u6267\u884c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7SSMS\u67e5\u770b\u5f97\u6570\u636e\u5e93<strong>\u5e76\u6ca1\u6709\u4efb\u4f55\u53d8\u5316<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a&ldquo;\u8fc1\u79fb&rdquo;\u547d\u4ee4\u5e76\u6ca1\u6709\u5bf9\u6570\u636e\u5e93\u6267\u884c\u4efb\u4f55SQL\u6307\u4ee4\u3002\u6b64\u65f6\uff0c\u5728\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0\u952e\u5165\u547d\u4ee4\uff1a<\/p>\n<pre class=\"language-bash\"><code>Update-database<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u6570\u636e\u5e93\u5c31\u4f1a\u795e\u5947\u5730\u6309\u7167WeatherTable\u6240\u58f0\u660e\u7684\u5f62\u5f0f\u5efa\u7acb\u8d77\u6570\u636e\u8868\u3002<\/p>\n<h2 id=\"\u5c0f\u7ed3\">\u5c0f\u7ed3<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\u6211\u4eec\u914d\u7f6e\u4e86\u6570\u636e\u5e93\u7684\u8fde\u63a5\u65b9\u5f0f\uff0c\u5e76\u501f\u7528EF\u8f7b\u677e\u5730\u6267\u884c\u4e86\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\u4e0b\u6587\u6211\u4eec\u5c06\u4f1a\u8fdb\u884c\u6570\u636e\u7684\u83b7\u53d6\u4e0e\u5199\u5165\u5e93\u7684\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"\u5f15\u7528\u53ca\u53c2\u8003\">\u5f15\u7528\u53ca\u53c2\u8003<\/h2>\n<ol>\n<li>\n<p>\u6768\u4e2d\u79d1.NET6\u89c6\u9891\u6559\u7a0b2022\u7248<a href=\"https:\/\/www.youtube.com\/playlist?list=PL9sJKk6XPMxehYCui7OysUV6trlBbJ4T_\">https:\/\/www.youtube.com\/playlist?list=PL9sJKk6XPMxehYCui7OysUV6trlBbJ4T_<\/a><\/p>\n<\/li>\n<li>\n<p>\u6768\u4e2d\u79d1.NET6\u89c6\u9891\u6559\u7a0bPart3-EFCore-2-\u5fd8\u6389DBFirst\u5427\uff0c\u6b63\u786e\u642d\u5efaEntity Framework Core<a href=\"https:\/\/www.youtube.com\/watch?v=cPpim_K2h9g\">https:\/\/www.youtube.com\/watch?v=cPpim_K2h9g<\/a><\/p>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wpai_generated_summary":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-100008","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":7}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/martinphysics.club\/?p=100008\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club\" \/>\n<meta property=\"og:description\" content=\"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14\" \/>\n<meta property=\"og:url\" content=\"https:\/\/martinphysics.club\/?p=100008\" \/>\n<meta property=\"og:site_name\" content=\"martinphysics.club\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-08T21:09:08+00:00\" \/>\n<meta name=\"author\" content=\"Martin Hung\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"Martin Hung\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008\"},\"author\":{\"name\":\"Martin Hung\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#\\\/schema\\\/person\\\/85413244415fa803d502f5febc10f3c5\"},\"headline\":\"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14\",\"datePublished\":\"2023-12-08T21:09:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008\"},\"wordCount\":177,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#\\\/schema\\\/person\\\/85413244415fa803d502f5febc10f3c5\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/martinphysics.club\\\/?p=100008#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008\",\"url\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008\",\"name\":\"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#website\"},\"datePublished\":\"2023-12-08T21:09:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/martinphysics.club\\\/?p=100008\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/?p=100008#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/martinphysics.club\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#website\",\"url\":\"https:\\\/\\\/martinphysics.club\\\/\",\"name\":\"martinphysics.club\",\"description\":\"\u7ad9\u70b9\u590d\u539f\u4e2d..\",\"publisher\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#\\\/schema\\\/person\\\/85413244415fa803d502f5febc10f3c5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/martinphysics.club\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/martinphysics.club\\\/#\\\/schema\\\/person\\\/85413244415fa803d502f5febc10f3c5\",\"name\":\"Martin Hung\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/martinphysics.club\\\/wp-content\\\/uploads\\\/2026\\\/08\\\/cropped-u16851548201322649963fm26gp0.jpg\",\"url\":\"https:\\\/\\\/martinphysics.club\\\/wp-content\\\/uploads\\\/2026\\\/08\\\/cropped-u16851548201322649963fm26gp0.jpg\",\"contentUrl\":\"https:\\\/\\\/martinphysics.club\\\/wp-content\\\/uploads\\\/2026\\\/08\\\/cropped-u16851548201322649963fm26gp0.jpg\",\"width\":512,\"height\":512,\"caption\":\"Martin Hung\"},\"logo\":{\"@id\":\"https:\\\/\\\/martinphysics.club\\\/wp-content\\\/uploads\\\/2026\\\/08\\\/cropped-u16851548201322649963fm26gp0.jpg\"},\"sameAs\":[\"http:\\\/\\\/martinphysics.club\"],\"url\":\"https:\\\/\\\/martinphysics.club\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/martinphysics.club\/?p=100008","og_locale":"zh_CN","og_type":"article","og_title":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club","og_description":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14","og_url":"https:\/\/martinphysics.club\/?p=100008","og_site_name":"martinphysics.club","article_published_time":"2023-12-08T21:09:08+00:00","author":"Martin Hung","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"Martin Hung","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/martinphysics.club\/?p=100008#article","isPartOf":{"@id":"https:\/\/martinphysics.club\/?p=100008"},"author":{"name":"Martin Hung","@id":"https:\/\/martinphysics.club\/#\/schema\/person\/85413244415fa803d502f5febc10f3c5"},"headline":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14","datePublished":"2023-12-08T21:09:08+00:00","mainEntityOfPage":{"@id":"https:\/\/martinphysics.club\/?p=100008"},"wordCount":177,"commentCount":0,"publisher":{"@id":"https:\/\/martinphysics.club\/#\/schema\/person\/85413244415fa803d502f5febc10f3c5"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/martinphysics.club\/?p=100008#respond"]}]},{"@type":"WebPage","@id":"https:\/\/martinphysics.club\/?p=100008","url":"https:\/\/martinphysics.club\/?p=100008","name":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14 - martinphysics.club","isPartOf":{"@id":"https:\/\/martinphysics.club\/#website"},"datePublished":"2023-12-08T21:09:08+00:00","breadcrumb":{"@id":"https:\/\/martinphysics.club\/?p=100008#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/martinphysics.club\/?p=100008"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/martinphysics.club\/?p=100008#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/martinphysics.club\/"},{"@type":"ListItem","position":2,"name":"\u4f7f\u7528.Net\u642d\u914dEF Core\u8bb0\u5f55\u4f60\u57ce\u5e02\u7684\u5929\u6c14"}]},{"@type":"WebSite","@id":"https:\/\/martinphysics.club\/#website","url":"https:\/\/martinphysics.club\/","name":"martinphysics.club","description":"\u7ad9\u70b9\u590d\u539f\u4e2d..","publisher":{"@id":"https:\/\/martinphysics.club\/#\/schema\/person\/85413244415fa803d502f5febc10f3c5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/martinphysics.club\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/martinphysics.club\/#\/schema\/person\/85413244415fa803d502f5febc10f3c5","name":"Martin Hung","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/martinphysics.club\/wp-content\/uploads\/2026\/08\/cropped-u16851548201322649963fm26gp0.jpg","url":"https:\/\/martinphysics.club\/wp-content\/uploads\/2026\/08\/cropped-u16851548201322649963fm26gp0.jpg","contentUrl":"https:\/\/martinphysics.club\/wp-content\/uploads\/2026\/08\/cropped-u16851548201322649963fm26gp0.jpg","width":512,"height":512,"caption":"Martin Hung"},"logo":{"@id":"https:\/\/martinphysics.club\/wp-content\/uploads\/2026\/08\/cropped-u16851548201322649963fm26gp0.jpg"},"sameAs":["http:\/\/martinphysics.club"],"url":"https:\/\/martinphysics.club\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/martinphysics.club\/index.php?rest_route=\/wp\/v2\/posts\/100008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/martinphysics.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/martinphysics.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/martinphysics.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/martinphysics.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100008"}],"version-history":[{"count":0,"href":"https:\/\/martinphysics.club\/index.php?rest_route=\/wp\/v2\/posts\/100008\/revisions"}],"wp:attachment":[{"href":"https:\/\/martinphysics.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/martinphysics.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/martinphysics.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}