无效的对象名称' dbo.customers1'

时间:2015-08-03 15:01:04

标签: sql-server asp.net-mvc entity-framework razor ef-database-first

我收到错误"无效的对象名称' dbo.customers1'"在我看来...

@foreach (var item in Model)
    {
        <tr>
            <td>@item.orderid</td>
            <td>@item.customer.firstname</td>

我有ViewModel类......

public class orders
{
    [Key]
    public int orderid { get; set; }
    public System.DateTime createdate { get; set; }
    public string createdby { get; set; }
    public Nullable<int> statusid { get; set; }
    public Nullable<System.DateTime> pickup { get; set; }
    public Nullable<System.DateTime> dropoff { get; set; }
    public Nullable<System.DateTime> scheduledout { get; set; }
    public Nullable<System.DateTime> scheduledin { get; set; }
    public bool instorepickup { get; set; }
    public string paymenttype { get; set; }
    public System.DateTime reservationstart { get; set; }
    public System.DateTime reservationend { get; set; }
    public bool morningpickup { get; set; }
    public Nullable<int> customerid { get; set; }
    public string notes { get; set; }
    public string shippingtype { get; set; }
    public Nullable<decimal> shippingestimate { get; set; }

    public virtual customer customer { get; set; }
    public virtual ICollection<invoice> invoices { get; set; }
    public virtual orderstatuses orderstatuses { get; set; }
    public virtual ICollection<serialorders> serialorders { get; set; }
}

public class customers
{
    [Key]
    public int customerid { get; set; }
    public System.DateTime createdate { get; set; }

    public string firstname { get; set; }

    public string lastname { get; set; }
    [Required]
    public string billingaddress1 { get; set; }
    public string billingaddress2 { get; set; }
    [Required]
    public string billingcity { get; set; }
    public string billingstate { get; set; }
    [Required]
    public string billingzip { get; set; }
    [Required]
    public string billingcountry { get; set; }
    public string phone { get; set; }
    [Required]
    public string email { get; set; }
    [Required]
    public string shippingaddress1 { get; set; }
    public string shippingaddress2 { get; set; }
    [Required]
    public string shippingcity { get; set; }
    public string shippingstate { get; set; }
    [Required]
    public string shippingzip { get; set; }
    [Required]
    public string shippingcountry { get; set; }
    public bool goodstanding { get; set; }
    public string userid { get; set; }
    public Nullable<DateTime> insuranceexp { get; set; }

    public virtual ICollection<invoice> invoices { get; set; }
    public virtual ICollection<order> orders { get; set; }
}

这是我的模特......

EF data model

我有一个数据访问层......

public DbSet<tvc.viewModels.orders> orders { get; set; }
public DbSet<tvc.viewModels.customers> customers { get; set; }

2 个答案:

答案 0 :(得分:0)

错误很简单。数据库中没有dbo.customers1表。至于为什么,这里真的没有足够的信息说,但最可能的原因是:

  1. 您正在使用Database First或Code First与现有数据库,并且您的实体与数据库不同步。

  2. 您正在使用“代码优先”,未启用迁移,并且您已对实体或数据库进行了更改。再次,不同步。

  3. 您已经通过Table属性或fluent配置指定了一个不存在的显式表名。无论哪种方式,都要将显式表名更改为应该的名称,或者重命名数据库表以使其匹配。

答案 1 :(得分:0)

我的ViewModel存在疏忽。我指的是客户,这是数据库表和模型类的名称,而不是客户(带有s)的viewmodel类。