I created a project using the newly Single page application of Asp.Net MVC 4. I ran the web app and the default Sql Server localDB database is created, as well as the Tables for the following class.
public class TodoItem
{
public int TodoItemId { get; set; }
[Required]
public string Title { get; set; }
public bool IsDone { get; set; }
// public string Test { get; set; }
[ForeignKey("TodoList")]
public int TodoListId { get; set; }
public virtual TodoList TodoList { get; set; }
}
Then
I found that I need the database generated varchar(max) for column Title. I modified it to varchar(500). (alter table todoitems alter column Title varchar(500) not null) The web app can be run without any problem.
Then I add a new property in the class (uncomment the Test property). I got the following exception when running the web app.
The model backing the 'TodoItemContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
on
[Authorize]
public class TodoListController : ApiController
{
private TodoItemContext db = new TodoItemContext();
// GET api/TodoList
public IEnumerable<TodoListDto> GetTodoLists()
{
return db.TodoLists.Include("Todos")
.Where(u => u.UserId == User.Identity.Name)
.OrderByDescending(u => u.TodoListId)
.AsEnumerable()
.Select(todoList => new TodoListDto(todoList));
}
I tried to modify the database table (
alter table todoitems add Test varchar(200)
), but the exception is still raised.
Is it possible to change the model class and modify (either by VisualStudio or manully) the table instead of recreate it? (I want to keep my manually changes on the database tables.)
Why I still get the exception after manually synchronized both side? I am using the newest single page application template and I updated all the referred packages using nuget.
ydbn
Member
6 Points
45 Posts
Code first, how to update model without recreate the table?
Feb 01, 2013 03:39 AM|LINK
I created a project using the newly Single page application of Asp.Net MVC 4. I ran the web app and the default Sql Server localDB database is created, as well as the Tables for the following class.
public class TodoItem { public int TodoItemId { get; set; } [Required] public string Title { get; set; } public bool IsDone { get; set; } // public string Test { get; set; } [ForeignKey("TodoList")] public int TodoListId { get; set; } public virtual TodoList TodoList { get; set; } }Then
on
[Authorize] public class TodoListController : ApiController { private TodoItemContext db = new TodoItemContext(); // GET api/TodoList public IEnumerable<TodoListDto> GetTodoLists() { return db.TodoLists.Include("Todos") .Where(u => u.UserId == User.Identity.Name) .OrderByDescending(u => u.TodoListId) .AsEnumerable() .Select(todoList => new TodoListDto(todoList)); }I tried to modify the database table (
), but the exception is still raised.
Is it possible to change the model class and modify (either by VisualStudio or manully) the table instead of recreate it? (I want to keep my manually changes on the database tables.)
thaicarrot
Contributor
5130 Points
1464 Posts
Re: Code first, how to update model without recreate the table?
Feb 01, 2013 03:52 AM|LINK
Yes, Actually, you can manually modify those. However, it you are using old EF, the EF also create metadata file associate with db.
You have to delete those metadata file.
Weera
ydbn
Member
6 Points
45 Posts
Re: Code first, how to update model without recreate the table?
Feb 01, 2013 04:21 AM|LINK
thaicarrot
Contributor
5130 Points
1464 Posts
Re: Code first, how to update model without recreate the table?
Feb 01, 2013 04:45 AM|LINK
weird issues, What version of EF?. Since there's many different EFs.
Weera