0

我正在尝试从我的控制器执行插入或更新,但我收到一条错误消息,指出“ICN.CourierClient 不包含我的各种属性(如“地址”)的定义 - 事实上它确实如此,以及我的其他类似调用到其他数据库模型可以使用相同的代码正常工作。我是否在某处错过了参考或某些配置,我在 dbset 语句中以与其他模型相同的方式定义它:

    public DbSet<Courier> Couriers { get; set; }
    public DbSet<Driver> Drivers { get; set; }
    public DbSet<Job> Jobs { get; set; }
    public DbSet<CourierClient> CourierClients { get; set; }

    IQueryable<Courier> Datasource.Couriers { get { return Couriers; } }
    IQueryable<Driver> Datasource.Drivers  { get { return Drivers; } }
    IQueryable<Job> Datasource.Jobs { get { return Jobs; } }
    IQueryable<CourierClient> Datasource.CourierClients { get { return CourierClients; } }

            IEnumerable<CourierClient> client = null;
            client = from c in db.CourierClients where c.ClientName == job.PickupCompanyName select c;

            if (client != null)
            {
                client.Address = job.PickupAddress1;
                client.City = job.PickupCity;
                client.State = job.PickupState;
                client.Zip = job.PickupZip;
                client.Phone = job.PickupPhone;
                db.Entry(client).State = EntityState.Modified;
            }

            else
            {
                client.ClientName = job.PickupCompanyName;
                client.Address = job.PickupAddress1;
                client.City = job.PickupCity;
                client.State = job.PickupState;
                client.Zip = job.PickupZip;
                client.Phone = job.PickupPhone;
                db.CourierClients.Add(client);
            }

            db.SaveChanges();

    public class CourierClient
{
    [Key]
    public virtual int ID { get; set; }

    public virtual string ClientName { get; set; }

    public virtual string ContactName { get; set; }

    public virtual string Address { get; set; }

    public virtual string City { get; set; }

    public virtual string State { get; set; }

    public virtual string Zip { get; set; }

    public virtual string Phone { get; set; }

    public virtual string Fax { get; set; }

    public virtual string Email { get; set; }

}
4

1 回答 1

0

您声明clientIEnumerable<CourierClient>. 这就是为什么编译器会告诉您它没有您希望在CourierClient.

        CourierClient client = (from c in db.CourierClients where c.ClientName == job.PickupCompanyName select c).SingleOrDefault();
        if (client != null)
        {
            client.Address = job.PickupAddress1;
            client.City = job.PickupCity;
            client.State = job.PickupState;
            client.Zip = job.PickupZip;
            client.Phone = job.PickupPhone;
            db.Entry(client).State = EntityState.Modified;
        }
        else
        {
            client = new CourierClient();
            client.ClientName = job.PickupCompanyName;
            client.Address = job.PickupAddress1;
            client.City = job.PickupCity;
            client.State = job.PickupState;
            client.Zip = job.PickupZip;
            client.Phone = job.PickupPhone;
            db.CourierClients.Add(client);
        }

        db.SaveChanges();

CourierClient请注意如果未找到实例的创建。

于 2013-08-11T01:55:54.747 回答