MVC4实体框架控制器数据库问题

时间:2013-08-11 01:43:14

标签: entity-framework asp.net-mvc-4

我正在尝试从我的控制器执行插入或更新,但是我收到一条错误消息,指出“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; }

}

1 个答案:

答案 0 :(得分:0)

您将client声明为IEnumerable<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实例,则创建该实例。