gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:48:44 24 4
gpt4 key购买 nike

我制作了一个 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; }
}
}

Controller (在 updateUser 时更改):

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();
}
}
}

数据库上下文:

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

关于c# - 如何使用代码优先迁移来更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com