杨柠 發表於 2025-6-18 12:04:00

Entity Framework Core中对实体类的字段迁移到数据库表

<p>环境:NET Core 7.0&nbsp; &nbsp;|&nbsp; Entity Framework Core 7.0.10&nbsp; &nbsp;|&nbsp;&nbsp;Sql Server 数据库</p>
<p>1、创建解决方案&nbsp;&nbsp;migration</p>
<p>2、创建类库&nbsp;test.Domain</p>
<p>  引用包:</p>
<p>  Microsoft.EntityFrameworkCore.Abstractions</p>
<p>  创建类&nbsp;AppUser.cs</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.EntityFrameworkCore;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.ComponentModel.DataAnnotations;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> test.Domain
{
   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> AppUser
    {
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 主键,自增行为
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
<span style="color: rgba(0, 0, 0, 1)">      
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span> Id { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 姓名
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span>? Name1 { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 身份证
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> IdNumber { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 性别男:1,女:2
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> Gender { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 出生日期
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> DateTime? Birthdate { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }      



    }
}</span></pre>
</div>
<p>  </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">| 特性名 | 说明 | 示例 | |---|---|---|
| | 如果是自增ID, 只需将主键属性声明为 int 或 long 类型,并加上 特性(或命名为 Id,EF Core 会自动识别。 | public int Id { get; set; } |
| | 指定属性为必填(非空),对应数据库的 NOT NULL。对于引用类型,不加问号即可达到同样效果。 | public string Name { get; set; } |
| | 指定字符串或数组的最大长度,映射为数据库字段长度。 | public string Name { get; set; } |
| | 指定字符串或数组的最小长度,仅用于模型验证,不影响数据库结构。 | public string Name { get; set; } |
| | 同时指定最大长度和可选的最小长度,主要用于模型验证。 | |
| | 指定数据库字段的备注(说明),EF Core 7+ 支持。 | |
| | 标记属性为并发检查字段。 | public int Version { get; set; } |
| 1777992885 | 用于乐观并发控制,通常用于 byte[] 类型的时间戳。 | 1777992885 public byte[] RowVersion { get; set; } |
| | 指定属性值由数据库生成(如自增主键)。 | |
| | 指定属性为数据库计算列。 | |
| | 指定外键属性。 | |
| | 指定导航属性的反向关系。 | |
| | 指定属性不映射到数据库表字段。 | public string Temp { get; set; } |
| | 指定数据库中的列名。 | |
| | 指定数据库中的表名(用于类)。 | |
| | 指定索引(EF Core 5+ 支持,推荐用 Fluent API)。 | |</span></pre>
</div>
<p>&nbsp;</p>
<p>3、创建类库&nbsp;test.EntityFrameworkCore</p>
<p>  引用包:</p>
<p>  Microsoft.EntityFrameworkCore.SqlServer</p>
<p>  Microsoft.EntityFrameworkCore.Design</p>
<p>  引用类库项目:</p>
<p>   test.Domain</p>
<p>  创建类&nbsp;MealDbContext.cs</p>
<p>  </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> test.Domain;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.EntityFrameworkCore;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> test.EntityFrameworkCore
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MealDbContext : DbContext
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> MealDbContext(DbContextOptions&lt;MealDbContext&gt; options) : <span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">(options)
      {
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> DbSet&lt;AppUser&gt; AppUsers { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }

      
      </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnModelCreating(ModelBuilder modelBuilder)
      {            
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">modelBuilder.Entity&lt;AppUser&gt;(b =&gt;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">{
            </span><span style="color: rgba(0, 128, 0, 1)">//</span>    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Indexes
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    b.HasKey(x =&gt; x.Id).IsClustered(false);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">});</span>
<span style="color: rgba(0, 0, 0, 1)">
            
      }
    }
}</span></pre>
</div>
<p>  创建类 DesignTimeDbContextFactory.cs</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.EntityFrameworkCore.Design;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.EntityFrameworkCore;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> test.EntityFrameworkCore
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> DesignTimeDbContextFactory : IDesignTimeDbContextFactory&lt;MealDbContext&gt;<span style="color: rgba(0, 0, 0, 1)">
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> DesignTimeDbContextFactory()
      {
      }

      </span><span style="color: rgba(0, 0, 255, 1)">public</span> MealDbContext CreateDbContext(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> optionsBuilder = <span style="color: rgba(0, 0, 255, 1)">new</span> DbContextOptionsBuilder&lt;MealDbContext&gt;<span style="color: rgba(0, 0, 0, 1)">();
            optionsBuilder.UseSqlServer(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Server=127.0.0.1;Database=MealDb;User Id=sa;Password=123456;Encrypt=False;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MealDbContext(optionsBuilder.Options);
      }
    }
}</span></pre>
</div>
<p>4、迁移命令:</p>
<p>  打开终端 执行命令:</p>
<p>  //添加migrations的up_appUser&nbsp; &nbsp; ,up_appUser是迁移名称,可自定义。</p>
<p>  D:\migration\test.EntityFrameworkCore&gt;<span style="color: rgba(51, 102, 255, 1)">dotnet ef migrations add up_appUser</span></p>
<p>  //同步到数据库</p>
<p>  D:\migration\test.EntityFrameworkCore&gt;<span style="color: rgba(51, 102, 255, 1)">dotnet ef database update</span></p>
<p>5、常见问题:</p>
<p><em id="__mceDel">  • 如果有多个 DbContext,可用 -Context 参数指定。<br>  •        迁移文件会生成在项目的 Migrations 文件夹下。<br>  •        修改实体后,重复执行&nbsp;<span style="color: rgba(51, 102, 255, 1)">dotnet ef migrations add xxx</span> 和&nbsp;<span style="color: rgba(51, 102, 255, 1)">dotnet ef database update</span> 即可同步结构。</em></p>
<p><em>  <em id="__mceDel">• 删除最新的migrations: <span style="color: rgba(51, 102, 255, 1)">dotnet ef migrations remove</span><br></em></em></p>

</div>
<div id="MySignature" role="contentinfo">
    有人问我,如果看不到确定的未来,还要不要付出。我只能说,并不是每一种付出都是在追寻结果。有时在付出的路上,能够收获的,是清楚地看到了自我想要的,或者不想要的,这又何尝不是一种宝贵的结果。命运会厚待温柔多情的人,好过冷漠的一颗心。<br><br>
来源:https://www.cnblogs.com/Chen-Jing-Hua/p/18934532
頁: [1]
查看完整版本: Entity Framework Core中对实体类的字段迁移到数据库表