如何使用代码优先迁移来更新数据库?

How to use code first migrations to update a database?

我制作了一个 c#.NET WebAPI 并且它可以工作,但是当我更改模型时它停止工作并给我以下错误消息。

The model backing the 'ApiDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database.

这是我的代码:

型号(不变):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        //public DateTime startTime { get; set; }
        //public DateTime endTime { get; set; }
        //public int Age { get; set; }
        //public string Adress { get; set; }
    }
}

模型(已更改):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime startTime { get; set; }
        //public DateTime endTime { get; set; }
        //public int Age { get; set; }
        //public string Adress { get; set; }
    }
}

控制器(在更新用户时更改):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Test.DBA;
using Test.Models;

namespace Test.Controllers
{
    public class UserAPIController : ApiController
    {
        ApiDbContext dbContext = null;
        public UserAPIController()
        {
            dbContext = new ApiDbContext();
        }
        [HttpPost]
        public IHttpActionResult InsertUser(User user)
        {
            dbContext.Users.Add(user);
            dbContext.SaveChangesAsync();

            return Ok(user.Name);
        }

        public IEnumerable<User> GetAllUser()
        {
            var list = dbContext.Users.ToList();
            return list;
        }

        [HttpPost]
        public IHttpActionResult DeleteUser(User user)
        {
            dbContext.Users.Remove(user);
            dbContext.SaveChanges();

            return Ok(user.Name);
        }

        [HttpGet]
        public IHttpActionResult ViewUser(int id)
        {
            var student = dbContext.Users.Find(id);
            return Ok(student);
        }

        [HttpPost]
        public IHttpActionResult UpdateUser(User user)
        {
            User std = dbContext.Users.Find(user.Id);

            std.Name = user.Name;
            std.startTime = user.startTime;
            //std.endTime = user.endTime;
            //std.Age = user.Age;
            //std.Adress = user.Adress;

            dbContext.Entry(std).State = System.Data.Entity.EntityState.Modified;
            dbContext.SaveChangesAsync();

            return Ok();
        }
    }
}

DBContext:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Test.Models;

namespace Test.DBA
{
    public class ApiDbContext :DbContext
    {
        public ApiDbContext() : base("Connection")
        {

        }
        public DbSet<User> Users { get; set; }
    }
}

连接字符串:

<connectionStrings>
   <add name ="Connection" connectionString="Data Source=.\SQLExpress;Initial Catalog=k;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

在 package-manager-console 中键入 Enable-Migrations,在键入 add-migration 后,设置名称,然后键入 update-database

https://msdn.microsoft.com/en-ca/data/jj591621(v=vs.113).aspx

  1. Enable-Migrations:通过创建配置 class.
  2. 在您的项目中启用迁移
  3. Add-Migration:使用 Up() 和 Down() 方法根据指定名称创建新迁移 class。
  4. Update-Database:执行由 Add-Migration 命令创建的最后一个迁移文件,并将更改应用于数据库架构。

更多详情: https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx