Saizaku Saizaku - 14 days ago 7
C# Question

How to use code first migrations to update a database?

I made a c#.NET WebAPI and it worked when I changed the model it stopped working and it gives me the following error message.

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

The Model(unchanged):

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


The Model(changed):

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; }
}
}
The Controller(changed at 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();
}
}
}


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


Connection string:

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

Answer

Type Enable-Migrations in package-manager-console, after type add-migration, set name, then type update-database

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

Comments