我被分配了一个由同事使用Entity框架编写的MVC应用程序。我们公司使用NHibernate,所以没有人真正理解基础实体框架,我们感到难过(谷歌搜索,搜索这里和其他网站到目前为止没有产生任何有用的信息)。
正在更新应用程序以显示更多信息。为此,需要一个日志,导致我创建一个新模型并尝试通过实体映射它。但是,在运行程序时,模型未在数据库中映射,并且手动创建它也无济于事。在加载控制器试图访问数据库中的表的页面时,运行程序只会给我一个错误。
错误:
{"Invalid object name 'dbo.CustomerHistories'."}
我正在尝试绘制的课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace CustomerManagement.Models
{
//save history of orders for each customer
public class CustomerHistory
{
[Key]
[ScaffoldColumn(false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Kunde")]
public int customerId { get; set; }
[Display(Name = "Voucher")]
public string voucherName{ get; set; }
[Display(Name = "Antal brugt")]
public int vouchersUsed { get; set; }
[Display(Name = "Event")]
public string theEvent { get; set; }
[Display(Name = "Bestillingstidspunkt")]
public DateTime orderedAt { get; set; }
}
}
在实体管理类中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace CustomerManagement.Models
{
public class CustomerManagementEntities : DbContext
{
*snip*
public DbSet<CustomerHistory> CustomerHistories { get; set; }
}
}
发生实际错误的代码:
List<CustomerHistory> histories = new List<CustomerHistory>();
histories = db.CustomerHistories.OrderBy(x=>x.customerId).ThenBy(x => x.orderedAt).ToList();
如果有人能帮助我,我将深表感谢,因为我似乎无法弄清楚发生了什么。