2014-04-17 14 views
5

Tôi đang cố gắng tạo danh sách thả xuống cho tên bộ phận. Tôi đang sử dụng MVC5. Tôi thấy quá nhiều giải pháp trên stack tràn nhưng tôi không bao giờ tìm thấy giải pháp có giá trị liên quan đến MVC5.Ngoại lệ loại 'System.InvalidOperationException' xảy ra trong EntityFramework.dll nhưng không được xử lý trong mã người dùng

Database Name : AppraisalDBContext 
Table Name : Department 
Column Name : deptID (Primarykey) 
       deptName (department name) 
       Description 

Tôi nhận được lỗi này:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code 

Additional information: The model backing the 'AppraisalDBContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/? 

Code:

điều khiển lớp Tên (DepartmentController.cs):

public ActionResult Index(string depName) 
{ 
    var DeptLst = new List<string>();   
    var GenreQry = from d in db.Department 
        orderby d.deptName 
        select d.deptName; 

    DeptLst.AddRange(GenreQry);    // Here I am confusing 
    ViewBag.depName = new SelectList(DeptLst); // Here I am confusing 
    var depts = from m in db.Department  // Here I am confusing 
       select m; 


    return View(depts); 
} 

lớp Model Name (Sở. cs):

namespace appraisalProject.Models 
{ 
    [Table("Department")] 
    public class Departments 
    { 
     [Key] 
     public int deptId { get; set; } 
     public string deptName { get; set; } 
     public string Description { get; set; } 
    } 
} 

lớp Model Name (AppraisalDBContext.cs):

namespace appraisalProject.Models 
{ 
    public class AppraisalDBContext:DbContext 
    { 
     public DbSet<Departments> Department { get; set; } 
    } 
} 

Index.cshtml:

@using (Html.BeginForm()) 
{ 
<p> 
    Genre: @Html.DropDownList("depts", "All") 
    <input type="submit" value="Filter" /> 
</p> 
} 

Trả lời

7

Các thông báo lỗi chi tiết tất cả những gì bạn cần biết:

Thông tin bổ sung: Mô hình ủng hộ ngữ cảnh 'AppraisalDBContext' đã thay đổi kể từ khi cơ sở dữ liệu được tạo. ing Code First Migrations để cập nhật cơ sở dữ liệu (http://go.microsoft.com/fwlink/?

Điều này có nghĩa là một trong các lớp học của bạn được sử dụng trong AppraisalDBContext đã thay đổi nhưng cơ sở dữ liệu chưa được cập nhật nên đã lỗi thời. Bạn cần phải cập nhật điều này bằng cách sử dụng di chuyển Code First.

Đây là một hình ảnh đẹp blogpost hướng dẫn bạn qua quy trình.

+0

Cảm ơn lan. Liên kết này cũng hữu ích cho tôi: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an -asp-net-mvc-ứng dụng –

1

câu trả lời của lan đã giúp tôi. Vấn đề là, tôi đã cập nhật các lớp mô hình nhưng cơ sở dữ liệu vẫn là định nghĩa cũ. Tôi nghĩ rằng Initializer ClearDatabaseSchemaIfModelChanges <> nên thiết lập lại cơ sở dữ liệu trên các cuộc gọi mới, thats những gì tôi đọc trên msdn. Hóa ra, initializer không được gọi. Vì vậy, tôi đã xóa cơ sở dữ liệu theo cách thủ công với:

new MyDbContext().Database.Delete(); 

trong thanh ghi WebApiConfig() Methode.

Sau đó, trình khởi tạo được gọi là khởi động tiếp theo và lược đồ db được cập nhật.

Các vấn đề liên quan